androidgoogle-mapslocationlistener

Update googlemap Camera location every time location changes


I would like the camera to automatically update and move every time the location changes. Is this possible? I am using onMapReady() and onLocationChanged().

public class MainActivity extends AppCompatActivity implements LocationListener, OnMapReadyCallback {

OnCreate()

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    locationManager = (LocationManager) (getSystemService(Context.LOCATION_SERVICE));


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        //TODO: Consider calling
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PICK_FINE_LOCATION_REQUEST);
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PICK_COARSE_LOCATION_REQUEST);
        return;
    }


    locationCurrent = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    onLocationChanged(locationCurrent);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, this);

     mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}

onMapReady()

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        //TODO: Consider calling
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PICK_FINE_LOCATION_REQUEST);
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PICK_COARSE_LOCATION_REQUEST);
        return;
    }
    LatLng latLng = new LatLng(locationCurrent.getLatitude(),locationCurrent.getLongitude());
    CameraPosition cameraPosition= new CameraPosition(latLng,15,0,0);
    CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
    googleMap.animateCamera(cameraUpdate);
    googleMap.setMyLocationEnabled(true);
    googleMap.getUiSettings().setZoomControlsEnabled(true);
    map = googleMap;

I have a global variable: Googlemap map;

onLocationChanged()

@Override
public void onLocationChanged(Location location) {

    //Log.d("UPDATEMAP","onLocationChanged");
    UpdateMap(map,location);}

I made a method called UpdateMap()

    public void UpdateMap(GoogleMap map,Location location){
    Log.d("UPDATEMAP","UpdateMap");
    LatLng latLngUpdate = new LatLng(location.getLatitude(),location.getLongitude());
    CameraPosition cameraPosition = new CameraPosition(latLngUpdate,15,0,0);
    CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
    map.animateCamera(cameraUpdate);
}

The error I am getting is that the global variable is equal to null even though I set it in onMapReady(). Any Help will be appreciated. Thanks Ritvik.


Solution

  • Your map variable is null because onLocationChanged is being called before onMapReady.
    To fix it request location updates in the method onMapReady after the variable is assigned.