Skip to content

Location layer doesn't display current location immediately. #424

@hangduykhiem

Description

@hangduykhiem

First off, thanks for the great work with the SDK and plugins.

Currently I'm running into this one problem trying to get user location to work. Basically, the Location Layer plugin doesn't immediately display the current location of user.
Sometimes you can force the location display by going out of the application and going in again, and sometimes you can just wait a bit for it to appear on the screen.

The behavior can be seen in this gif here.

ezgif-1-e331f3787d

Here is my setup function:

    private void setupMapView() {
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(final MapboxMap mapboxMap) {
                if (PermissionsManager.areLocationPermissionsGranted(MainActivity.this)) {
                    locationEngine = new LocationEngineProvider(MainActivity.this).obtainLocationEngineBy(LocationEngine.Type.ANDROID);
                    locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
                    locationEngine.addLocationEngineListener(new LocationEngineListener() {
                        @Override
                        public void onConnected() {
                            locationEngine.requestLocationUpdates();
                        }

                        @Override
                        public void onLocationChanged(Location location) {
                            setCameraPosition(mapboxMap, location);
                            locationEngine.removeLocationEngineListener(this);
                        }
                    });
                    locationEngine.activate();
                    locationPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine);
                }

                mapboxMap.getUiSettings().setCompassEnabled(false);
                mapboxMap.getUiSettings().setRotateGesturesEnabled(false);
                mapboxMap.getUiSettings().setTiltGesturesEnabled(false);
            }
        });
    }


    private void setCameraPosition(MapboxMap map, Location location) {
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(location.getLatitude(), location.getLongitude()), 13));
    }

    @Override
    @SuppressWarnings({"MissingPermission"})
    protected void onStart() {
        super.onStart();
        if (locationEngine != null) {
            locationEngine.requestLocationUpdates();
        }
        if (locationPlugin != null) {
            locationPlugin.onStart();
        }
        mapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (locationEngine != null) {
            locationEngine.removeLocationUpdates();
        }
        if (locationPlugin != null) {
            locationPlugin.onStop();
        }
        mapView.onStop();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    location-layer-pluginIssues that deal with the location layer module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions