简体   繁体   中英

Null pointer exception while getting current location in android google maps

I am trying to get the user's current location and zoom the camera at the location by using Google Maps. It throws a null pointer exception at this line, String provider = locationManager.getBestProvider(criteria, true); How do I resolve this?

public class ParkFinderMapView extends SupportMapFragment implements OnMapReadyCallback, LocationListener{

private GoogleMap mMap;
private LocationManager locationManager;
private static final long MIN_TIME = 400;
private static final float MIN_DISTANCE = 1000;

public ParkFinderMapView() {
}

@Override
public void onResume() {
    super.onResume();

    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {

    if (mMap == null) {

        getMapAsync(this);
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;

    setUpMap();
}

private void setUpMap() {


    // Enable MyLocation Layer of Google Map 
    mMap.setMyLocationEnabled(true); 

    // Create a criteria object to retrieve provider 
    Criteria criteria = new Criteria(); 

    //for fragments -include getActivity
    LocationManager mgr = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

    // Get the name of the best provider 
    String provider = locationManager.getBestProvider(criteria, true); 

    // Get Current Location 
    Location myLocation = locationManager.getLastKnownLocation(provider); 



    // set map type 
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

    // Get latitude of the current location 
    double latitude = myLocation.getLatitude(); 

    // Get longitude of the current location 
    double longitude = myLocation.getLongitude(); 

    // Create a LatLng object for the current location 
    LatLng latLng = new LatLng(latitude, longitude); 

    // Show the current location in Google Map 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

    // Zoom in the Google Map 
    mMap.animateCamera(CameraUpdateFactory.zoomTo(14)); 
    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located")); 

    LatLng myCoordinates = new LatLng(latitude, longitude); 
    CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(myCoordinates, 12); 
    mMap.animateCamera(yourLocation); 

    CameraPosition cameraPosition = new CameraPosition.Builder() 
    .target(myCoordinates)      // Sets the center of the map to LatLng (refer to previous snippet) 
    .zoom(17)                   // Sets the zoom 
    .bearing(90)                // Sets the orientation of the camera to east 
    .tilt(30)                   // Sets the tilt of the camera to 30 degrees 
    .build();                   // Creates a CameraPosition from the builder 
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

}


@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
    mMap.animateCamera(cameraUpdate);
    locationManager.removeUpdates(this);

}

}

Try this:

LocationManager locationManager = (LocationManager)      getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);

You declare a private member:

private LocationManager locationManager;

Later when getting the location manager object you assign it to a local variable:

LocationManager mgr = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);

It should be

locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM