I have an Activity that gets position with fused location and puts it in two text boxes.
In a frame, I have a Google map.
In two text boxes, I have my latitude and longitude.
I want map center to adjust as location changes.
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
latitud.setText(String.valueOf(mCurrentLocation.getLatitude()));
longitud.setText(String.valueOf(mCurrentLocation.getLongitude()));
tiempo.setText(mLastUpdateTime);
}
My idea was adding to onLocationChanged something like this:
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
latitud.setText(String.valueOf(mCurrentLocation.getLatitude()));
longitud.setText(String.valueOf(mCurrentLocation.getLongitude()));
tiempo.setText(mLastUpdateTime);
LatLng mapCenter = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLatitude());
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mapCenter, 16));
// Flat markers will rotate when the map is rotated,
// and change perspective when the map is tilted.
googleMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.flecha))
.position(mapCenter)
.flat(true)
.rotation(245));
CameraPosition cameraPosition = CameraPosition.builder()
.target(mapCenter)
.zoom(16)
.bearing(90)
.build();
// Animate the change in camera view over 2 seconds
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),
2000, null);
}
But is giving me the following error:
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