-1

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

enter image description here

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));
}

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mohamed Wessam
  • 167
  • 2
  • 12

1 Answers1

0

Are you sure your onLocationUpdate function is executed?

It's perfectly normal to get a crask when calling moveCamera from your onCreate function because the Camera object doesn't exist yet. It only exists when the onMapReady is executed.

Zelig63
  • 1,592
  • 1
  • 23
  • 40
  • the app crash also when I call moveCamera inside onMapReady, I'm not sure what you mean by "executed", but onLocationChanged works will and give me the right location. – Mohamed Wessam Jul 13 '19 at 13:09
  • I meant trigerred each time a new location is measured. Can you post the error message you get when the call to `moveCamera` in the `onMapReady` crashes? – Zelig63 Jul 14 '19 at 07:32