I want google maps to show the location of the user. I tried this code, but it did not work on Android 6.
private GoogleMap map;
LocationManager lm;
LocationListener ll;
Location l;
LatLng pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_layout);
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
ll = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
l = (Location) location;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.nMap);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
lm.requestLocationUpdates(lm.NETWORK_PROVIDER, 0, 0, ll);
}
pos = new LatLng(l.getLatitude(), l.getLongitude());
// Add a marker in Sydney and move the camera
map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions().position(pos).title("Marker in Sydney"));
map.moveCamera(CameraUpdateFactory.newLatLng(pos));
}
Here are the permissions I've set:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
Firstly make sure your API Key is valid and add this into your manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Here's my maps activity.. there might be some redundant information in it since it's from a larger project I created.
There's a lot of methods here that are hard to understand but basically all update the map when it's paused etc. There are also connection timeouts etc. Sorry for just posting this, I tried to fix your code but I couldn't figure out what was wrong.
Sorry but that's just much too much overhead (above), short and quick, if you have the MapFragment, you also have to map, just do the following:
Code is in Kotlin, hope you don't mind.
have fun
Btw I think this one is a duplicate of: Show Current Location inside Google Map Fragment
For using FusedLocationProviderClient with Google Play Services 11 and higher:
see here: How to get current Location in GoogleMap using FusedLocationProviderClient
For using (now deprecated) FusedLocationProviderApi:
If your project uses Google Play Services 10 or lower, using the FusedLocationProviderApi is the optimal choice.
The FusedLocationProviderApi offers less battery drain than the old open source LocationManager API. Also, if you're already using Google Play Services for Google Maps, there's no reason not to use it.
Here is a full Activity class that places a Marker at the current location, and also moves the camera to the current position.
It also checks for the Location permission at runtime for Android 6 and later (Marshmallow, Nougat, Oreo). In order to properly handle the Location permission runtime check that is necessary on Android M/Android 6 and later, you need to ensure that the user has granted your app the Location permission before calling
mGoogleMap.setMyLocationEnabled(true)
and also before requesting location updates.activity_main.xml:
Result:
Show permission explanation if needed using an AlertDialog (this happens if the user denies a permission request, or grants the permission and then later revokes it in the settings):
Prompt the user for Location permission by calling
ActivityCompat.requestPermissions()
:Move camera to current location and place Marker when the Location permission is granted: