0

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));

}
Soufyane Kaddouri
  • 315
  • 3
  • 6
  • 14
  • Possible duplicate of [getLastKnownLocation returns null](http://stackoverflow.com/questions/20438627/getlastknownlocation-returns-null) – Morrison Chang Jul 15 '16 at 19:45
  • The problem is that getLastKnownLocation can legitimately return null. Not going to close it as a dupe because I think the one mentioned so far is bad and I don't have time to look for a good one. – Gabe Sechan Jul 15 '16 at 19:53

0 Answers0