我有这种问题:我在Fragment中的应用程序中使用GoogleMaps地图,并且在调用onLocationChanged方法时会中断。 问题是这条线
mMap.moveCamera(CameraUpdatesFactory.newLatLngZoom(position,15);
可能是因为找不到mMap或我不知道。 这是代码部分:
public class home extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener{
GoogleMap mMap;
Marker marker;
private void checkAndStartLocationUpdate() {
if (permissionGranted && googleApiClientReady) {
Log.d("MainActivity", "Start updating location");
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
try {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} catch (SecurityException e) {
e.printStackTrace();
}
}
}
public void onLocationChanged(Location location) {
Log.d("MainActivity", "Location update received: " + location.toString());
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("MainActivity", longitude + " " + latitude);
LatLng position = new LatLng(longitude, latitude);
marker.setPosition(position);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));
}
答案 0 :(得分:0)
您可以选择
@Override
public void onMapReady(GoogleMap googleMap)
{
mMap = googleMap;
// Tika
// Tika
// Tika
}
并更改
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));
到
if(mMap != null)
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));
或者您可以简单地致电
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
在onMapReady
之后的mMap = googleMap;
内部