nasch
nasch

Reputation: 5498

Mabox Android - not getting location updates

I'm using the Mapbox SDK and I want to move and zoom the map to the current location. If I enable the location layer, I get the dot in the correct spot. However my location engine listener isn't being called so I can't move the camera.

In onCreateView I start a connection with a GoogleApiClient. Once that's connected I use it to check the location settings to see if they're turned on (after checking for permission for location). If that succeeds, I do this:

if (locationEngine == null) {
   locationEngine = new LocationSource(getActivity());
   locationEngine.requestLocationUpdates();
}
Location location = locationEngine.getLastLocation(); // This returns null

onLocationChanged(location); // This moves the camera if a location is passed in

locationEngine.addLocationEngineListener(locationEngineListener);

// This works, the location layer functions properly    
getMap(new OnMapReadyCallback() {
            @Override
            public void onMapReady(MapboxMap mapboxMap) {

                mapboxMap.setMyLocationEnabled(true);
            }
        });

Here's what locationEngineListener does:

private class Listener implements LocationEngineListener {
        @Override
        public void onConnected() {
            // No action needed here.
        }

        @Override
        public void onLocationChanged(Location location) {

            CurrentLocationMap.this.onLocationChanged(location);
        }
    }

The problem is onLocationChanged is never called.

Upvotes: 0

Views: 1057

Answers (1)

Uddhav P. Gautam
Uddhav P. Gautam

Reputation: 7626

This is the exactly working solution, probaly in slight different circumstances. But I wanted to add some little explanation steps so that anybody gets the exact concepts. I don't see what LocationEngine you are using in your code. Here I am using LocationServices.FusedLocationApi as my LocationEngine.

1) onCreate() of Android Component (Eg, Activity, Fragment or Service. Note: Not IntentService), build and then connect the GoogleApiClient as below.

buildGoogleApiClient();
mGoogleApiClient.connect();

where, buildGoogleApiClient() implementation is,

protected synchronized void buildGoogleApiClient() {
        Log.i(TAG, "Building GoogleApiClient");

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

    }

Later on onDestroy(), you can disconnect GoogleApiClient as,

@Override
    public void onDestroy() {
        Log.i(TAG, "Service destroyed!");
        mGoogleApiClient.disconnect();
        super.onDestroy();
    }

The step 1 makes sure you build and connect the GoogleApiClient.

1) GoogleApiClient instance first time gets connected on method onConnected(). Now, your next step should look onConnected() method.

@Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.i(TAG, "GoogleApiClient connected!");
        buildLocationSettingsRequest();
        createLocationRequest();
        location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        Log.i(TAG, " Location: " + location); //may return **null** because, I can't guarantee location has been changed immmediately 
    }

Above, you called a method createLocationRequest() to create location request. The method createLocationRequest() looks like below.

protected void createLocationRequest() {
        //remove location updates so that it resets
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); //Import should not be **android.Location.LocationListener**
    //import should be **import com.google.android.gms.location.LocationListener**;

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        //restart location updates with the new interval
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

    }

3) Now, on onLocationChange() callback of LocationListener interface, you get new location.

@Override
    public void onLocationChanged(Location location) {
        Log.i(TAG, "Location Changed!");
        Log.i(TAG, " Location: " + location); //I guarantee,I get the changed location here

    }

You get the result like this in Logcat: 03-22 18:34:17.336 817-817/com.LiveEarthquakesAlerts I/LocationTracker: Location: Location[fused 37.421998,-122.084000 acc=20 et=+15m35s840ms alt=0.0]

To be able to do these three steps, you should have configured your build.gradle as below:

 compile 'com.google.android.gms:play-services-location:10.2.1'

Upvotes: 2

Related Questions