2017-04-08 50 views
-1

我已经尝试了前面提到的解决方案。谷歌地图 - 空指针异常

public class LocationActivity extends FragmentActivity implements 
       LocationListener, 
       GoogleApiClient.ConnectionCallbacks, 
       GoogleApiClient.OnConnectionFailedListener, OnMapReadyCallback { 

     private static final String TAG = "LocationActivity"; 
     private static final long INTERVAL = 1000 * 60 * 1; //1 minute 
     private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute 
     Button btnFusedLocation; 
     TextView tvLocation; 
     LocationRequest mLocationRequest; 
     GoogleApiClient mGoogleApiClient; 
     Location mCurrentLocation; 
     String mLastUpdateTime; 
     GoogleMap googleMap; 

     protected void createLocationRequest() { 
      mLocationRequest = new LocationRequest(); 
      mLocationRequest.setInterval(INTERVAL); 
      mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
      mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     } 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      if (!isGooglePlayServicesAvailable()) { 
       finish(); 
      } 
      createLocationRequest(); 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addApi(LocationServices.API) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .build(); 

      setContentView(R.layout.activity_location_google_map); 
      SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
      fm.getMapAsync(this); 
     } 

     @Override 
     public void onMapReady(GoogleMap map) { 

      map.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
      map.setMyLocationEnabled(true); 
      map.setTrafficEnabled(true); 
      map.setIndoorEnabled(true); 
      map.setBuildingsEnabled(true); 
      map.getUiSettings().setZoomControlsEnabled(true); 
     } 


     private void addMarker() { 
      MarkerOptions options = new MarkerOptions(); 
      IconGenerator iconFactory = new IconGenerator(this); 
      iconFactory.setStyle(IconGenerator.STYLE_PURPLE); 
      options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(mLastUpdateTime))); 
      options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV()); 

      LatLng currentLatLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()); 
      options.position(currentLatLng); 
      Marker mapMarker = googleMap.addMarker(options); 
      long atTime = mCurrentLocation.getTime(); 
      mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime)); 
      mapMarker.setTitle(mLastUpdateTime); 
      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 
        13)); 
     } 

     } 
    } 

我想添加一个带有标签的标记作为使用GPS测量纬度和经度的时间。但是,当这个方法被调用时,我得到一个错误:addMarker(..)在空对象上被调用。在调试模式下运行时经纬度显示正确。所以,显然地图本身是空的。有人能告诉我如何着手解决这个问题吗?我已经给出了相关的代码。感谢提前:)

+0

检查出在这里:http://stackoverflow.com/questions/38388282/finding-current-location-of-用户在android/38397092#38397092 –

+0

从代码中不清楚你在何时何地调用addMarker函数。它是否在onMapReady被调用之后? –

+0

@Override public void onLocationChanged(Location location){ mCurrentLocation = location; mLastUpdateTime = DateFormat.getTimeInstance()。format(new Date()); addMarker(); } – Jab

回答

0

初始化GoogleMap的onMapReady

googleMap = map;

@Override 
    public void onMapReady(GoogleMap map) { 
     googleMap = map; 
     map.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     map.setMyLocationEnabled(true); 
     map.setTrafficEnabled(true); 
     map.setIndoorEnabled(true); 
     map.setBuildingsEnabled(true); 
     map.getUiSettings().setZoomControlsEnabled(true); 
    } 
+0

谢谢。这工作:) – Jab