0

I'm trying to get the user location using LocationEngine on Android devices , but when I star the app the usser's marker is located in (0.0, 0.0) and never update it's location. How is it possible?

I try to replicate the example location Layer

private void enableLocationPlugin() {

    // Check if permissions are enabled and if not request
    if (PermissionsManager.areLocationPermissionsGranted(this)) {

        // Create an instance of LOST location engine
        initializeLocationEngine();
        LocationLayerOptions options = LocationLayerOptions.builder(this)
                .build();
        locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine, options);
        locationLayerPlugin.setRenderMode(RenderMode.COMPASS);
        locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
        locationLayerPlugin.setLocationLayerEnabled(true);
    } else {
        permissionsManager = new PermissionsManager(this);
        permissionsManager.requestLocationPermissions(this);
    }
}


private void initializeLocationEngine() {
    locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
    locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
    locationEngine.setInterval(1000);
    locationEngine.setFastestInterval(500);
    locationEngine.addLocationEngineListener(this);
    locationEngine.activate();
    locationEngine.requestLocationUpdates();


    Location lastLocation = locationEngine.getLastLocation();
    if (lastLocation != null) {
        setCameraPosition(lastLocation);
        locationEngine.addLocationEngineListener(this);
    } else {
        locationEngine.addLocationEngineListener(this);
    }
}

I'm using this version of MapBox in grandle

implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-building:0.2.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.12.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-locationlayer:0.5.2'

Polivicio
  • 71
  • 1
  • 1
  • 12

2 Answers2

2

I had the same problem. The problem is you're trying to get high accuracy location. High accuracy should make use of a combination of GPS, Cellular data, WiFi, etc to get the most accurate location; but for some reason that I don't know, it doesn't work.

There are four options:

NO_POWER

LOW_POWER

BALANCED_POWER_ACCURACY

HIGH_ACCURACY

I changed this line of my code:

locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);

to this:

locationEngine.setPriority(LocationEnginePriority.BALANCED_POWER_ACCURACY);

and it worked!

Community
  • 1
  • 1
mrazizi
  • 319
  • 2
  • 15
0

Few notes about your code snippet.

LocationLayerOptions options = LocationLayerOptions.builder(this)
            .build();

isn't needed since you aren't actually changing any options.

locationLayerPlugin.setLocationLayerEnabled(true);

Isn't needed since you are calling setRenderMode right above which automatically enables it

The issue with seeing the user location at nullisland (0.0, 0.0) has been reported already in https://github.com/mapbox/mapbox-plugins-android/issues/460.

For the location updates, it looks like you are attaching a listener. are you receiving updates in that listener and the Location Layers just not updating or are you not receiving any updates at all? If the latter, it's an issue more likely with the locationEngine and not the Location Layer Plugin.

cammace
  • 3,138
  • 1
  • 13
  • 18
  • I resolved adding locationEngine.addLocationEngineListener(this); in locationLayerPlugin initialization, it seems to work ... – Polivicio May 19 '18 at 08:41