尝试在Google地图上的当前位置设置标记会产生Nullpointer异常

时间:2014-07-18 10:55:51

标签: android google-maps nullpointerexception locationmanager

我在我的Android项目中集成了Google Maps。我正在设备上查看地图。我想将标记设置为我当前的位置。我已完成以下编码,但它在第43行给出了Null Pointer Exception,这是以下行

mMap.addMarker(new MarkerOptions() .position(new LatLng(location.getLatitude(), location.getLongitude())) .title("Hello world"));

我的代码如下。请一步一步指导我出错的地方。

     public class location extends Activity implements LocationListener {
private GoogleMap mMap;
LocationManager locationManager;
private static final long MIN_TIME = 400;
private static final float MIN_DISTANCE = 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_location);

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.the_map)).getMap();
    mMap.setMyLocationEnabled(true);

    //mMap.addMarker(new MarkerOptions());

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this); 
    Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    mMap.addMarker(new MarkerOptions()
    .position(new LatLng(location.getLatitude(), location.getLongitude()))
    .title("Hello world"));


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


}

3 个答案:

答案 0 :(得分:1)

尝试以下适用于我的代码..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_location);
    // Getting Google Play availability status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();

    }else { // Google Play Services are available

        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();

        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);

        // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

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

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

        // Getting Current Location
        Location location = locationManager.getLastKnownLocation(provider);

        LocationListener locationListener = new LocationListener() {
          void onLocationChanged(Location location) {
          // redraw the marker when get location update.
          drawMarker(location);
        }

        if(location!=null){
           //PLACE THE INITIAL MARKER              
           drawMarker(location);
        }
        locationManager.requestLocationUpdates(provider, 20000, 0, locationListener);
    }
}

private void drawMarker(Location location){
    googleMap.clear();
    LatLng currentPosition = new LatLng(location.getLatitude(),location.getLongitude());
    googleMap.addMarker(new MarkerOptions()
    .position(currentPosition)
    .snippet("Lat:" + location.getLatitude() + "Lng:"+ location.getLongitude()));
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
    .title("ME"));
}

答案 1 :(得分:0)

Ur代码似乎是正确的。只需检查您的位置对象,它可能为空。

答案 2 :(得分:0)

@Override
public void onLocationChanged(Location location) {
    // Add a marker in Sydney and move the camera
    mLocation = location;
    LatLng myLocation = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());
    mMap.addMarker(new MarkerOptions()
            .position(myLocation)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
            .title("My Location"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation));
    Log.d("location", "Latitude:" + mLocation.getLatitude() + "\n" + "Longitude:" + mLocation.getLongitude());
}