【问题标题】:Null pointer exception while getting current location in android google maps在 android 谷歌地图中获取当前位置时出现空指针异常
【发布时间】:2015-10-08 14:53:29
【问题描述】:

我正在尝试使用 Google 地图获取用户的当前位置并在该位置缩放相机。它在这一行抛出了一个空指针异常,String provider = locationManager.getBestProvider(criteria, true);我该如何解决这个问题?

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

}

}

【问题讨论】:

    标签: android eclipse google-maps-api-3 google-maps-android-api-2


    【解决方案1】:

    你声明了一个私有成员:

    private LocationManager locationManager;
    

    稍后在获取位置管理器对象时将其分配给局部变量:

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

    应该是

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

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

      • 在您的原始代码中,而不是使用 String provider = locationManager.getBestProvider(criteria, true);这个只是替换 locationManager。与 mgr 即 String provider = mgr.getBestProvider(criteria, true);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多