2014-07-18 26 views
-1

我已經在我的Android項目中集成了Google Maps。我在設備上看到了地圖的視圖。我想將標記設置爲當前位置。我做了以下的編碼,但它給了我上線43 Null Pointer Exception是以下行試圖在我的谷歌地圖上的當前位置設置標記給Nullpointer異常

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


} 

回答

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")); 
} 
0

Ur代碼似乎是正確的。只需檢查你的位置對象,它可能爲空。

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()); 
} 
相關問題