Unable To Show Current Location On Google Map?
Solution 1:
I tried out your code on a device, and I was able to see the current location AFTER moving around for a little (I think the code triggered onLocationChanged
). It showed the blue current location marker, which verifies that your code for the Maps
works fine IF triggered properly.
About the code you posted, noticed that you're calling return;
after this if statement
:
if(ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED) {
-- this makes the code below it, unreachable. I think that is a factor as to why the current location doesn't show up for some reason. After modifying it (removed return;
), I was able to proceed with the code below it:
Log.i(""MAP"",""BEFORE LOCATION"");
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
Log.i(""MAP"", ""AFTER LOCATION"");
LocationManagerlocationManager= (LocationManager) getActivity().getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Criteriacriteria=newCriteria();
StringbestProvider= locationManager.getBestProvider(criteria, true);
Locationlocation= locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
Log.i(""MAP"", ""END LOCATION"");
-- here, I just added a Log
to see what the value of location is which turns out to be null
(tested it first using an emulator). I think this causes the map to NOT show a current location marker. The only permissions I added in my manifest were ACCESS_COARSE_LOCATION
, ACCESS_FINE_LOCATION
, and WRITE_EXTERNAL_STORAGE
.
PS: After I removed the
return;
in yourif statement
, I got an error that was related with the permissions. What I did was follow the fastest way from this answer (I just modified my targetSdkVersion to 22).
Decided to test it out in a device from here on. At first, the map was able to load but did NOT show any current location marker, it just showed the locate me button in the upper right corner of the map, I also added a Toast
to show the value of location, which still shows as null
. Then I went and move around for sometime and that's when the map camera directed and zoomed in towards my current location.
During testing on both emulator and device, Location was turned on. :)
So I think the main concern for you here is NOTnot being able to show current location on maps but unable to pass the correct data where maps should direct. I looked around for a while and seen some examples that are quite similar to your code, they just have a few more included in it. The sample project mentioned in this answer is a good example on how to use Location
(the answer itself is useful).
Hope this helps. Good luck.
Post a Comment for "Unable To Show Current Location On Google Map?"