This activity is to get current user location and update it every 10 sec, the problem is that:
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
doesn't work and map appear like this
and if I move this line to onCreate the app crash with error message:
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)
What should I do to make the map open and zoom to my location?
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));
}
}