I was struggling very long to fix a users location in a Google maps activity. When i runned it once it worked perfectly, the marker was spotted at the users location. But now i ran it again and the app stops with two errors in the stack-trace.
Here are the errors:
at com.doppler.stackingcoder.pechhulp.PechhulpActivity.onLocationChanged(PechhulpActivity.java:87)
at com.doppler.stackingcoder.pechhulp.PechhulpActivity.onMapReady(PechhulpActivity.java:101)
And here are the two methods which have the errors:
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude(); // Line 87
longtitude = location.getLongitude();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.moveCamera(CameraUpdateFactory.zoomTo(10));
mMap.getUiSettings().setRotateGesturesEnabled(false);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
onLocationChanged(location); // Line 101
LatLng myPosition = new LatLng(latitude, longtitude);
mMap.addMarker(new MarkerOptions().position(myPosition)
.title("Uw Locatie:")
.snippet("Dit is uw locatie")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_mini))
);
mMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
}