此活动是获取当前用户位置并每10秒更新一次,问题是:
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
不起作用,地图显示如下
,如果我将此行移到onCreate,则应用程序崩溃并显示错误消息:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at com.ENG.Activities.Maps.UserLocation.onCreate(UserLocation.java:56)
我应该怎么做才能打开地图并缩放到我的位置?
UserLocationActivity
public class UserLocation extends AppCompatActivity implements OnMapReadyCallback,
LocationListener, GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private FusedLocationProviderClient fusedLocationClient;
private LocationRequest mLocationRequest;
private Location mLastLocation;
private LatLng latLng;
private double lat, lng;
private Marker mCurrentUserLocationMarker;
private final int UPDATE_LOCATION_TIME = 10000;
private final int FASTEST_LOCATION_TIME = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_location);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_current_location);
mapFragment.getMapAsync(this);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
private synchronized void buildLocationRequest() {
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(UPDATE_LOCATION_TIME)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setFastestInterval(FASTEST_LOCATION_TIME);
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
buildLocationRequest();
}
@Override
public void onConnectionSuspended(int i) {
buildGoogleApiClient();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(getApplicationContext(), "Failed to connect", Toast.LENGTH_LONG).show();
buildGoogleApiClient();
}
@Override
public void onLocationChanged(Location location) {
this.mLastLocation = location;
if (mCurrentUserLocationMarker != null) {
mCurrentUserLocationMarker.remove();
}
lat = mLastLocation.getLatitude();
lng = mLastLocation.getLongitude();
latLng = new LatLng(lat, lng);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng)
.title("Current user Location")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
mCurrentUserLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
}
}
答案 0 :(得分:0)
您确定您的onLocationUpdate
函数已执行吗?
从您的moveCamera
函数调用onCreate
时出现a行是很正常的,因为Camera
对象尚不存在。它仅在执行onMapReady
时存在。