0

I'm using MapBox latest lib (0.22.0). I'm trying to rotate the map to always face the direction we are moving towards to with MapBox Android.

This is my code:

@Override
public void onMapReady(MapboxMap mapboxMap) {
    this.mapboxmap = mapboxMap;
    mapboxMap.setStyle(Style.OUTDOORS);
    mapboxMap.getUiSettings().setAttributionEnabled(false);
    mapboxMap.getUiSettings().setCompassEnabled(true);
    mapboxMap.getUiSettings().setLogoEnabled(false);
    mapboxMap.getUiSettings().setRotateGesturesEnabled(true);
    mapboxMap.getUiSettings().setZoomControlsEnabled(true);
    initLocationEngine();
    initLocationLayer();
}

private void initLocationLayer() {
   /* LocationLayerPlugin locationLayer = new LocationLayerPlugin(mapView, mapboxmap, locationEngine);
    locationLayer.setRenderMode(RenderMode.GPS);*///(Old Lib Code 0.19.0)

    LocationComponent locationComponent = mapboxmap.getLocationComponent();
    locationComponent.activateLocationComponent(getActivity(), locationEngine);
    locationComponent.setLocationComponentEnabled(true);
    locationComponent.setCameraMode(CameraMode.TRACKING);
    locationComponent.setRenderMode(RenderMode.COMPASS);
    locationComponent.zoomWhileTracking(30);
}

@SuppressLint("MissingPermission")
private void initLocationEngine() {

    locationEngine = new  LocationEngineProvider(activity).obtainBestLocationEngineAvailable();
    locationEngine.setPriority(HIGH_ACCURACY);
    locationEngine.setInterval(0);
    locationEngine.setFastestInterval(1000);
    locationEngine.addLocationEngineListener(this);

  //userTrackingMode
       locationEngine.activate();
        if (locationEngine.getLastLocation() != null) {
        Location lastLocation = locationEngine.getLastLocation();

        onLocationChanged(lastLocation);
        currentLocation = Point.fromLngLat(lastLocation.getLongitude(), lastLocation.getLatitude());

    }
}
Morphed
  • 3,527
  • 2
  • 29
  • 55

1 Answers1

0

With this code snippet, the icon should have a small arrow reflecting the compass bearing.

If the issue here is the map camera behavior, then you need to set the camera mode to TRACKING_COMPASS instead of TRACKING:

    locationComponent.setCameraMode(CameraMode.TRACKING_COMPASS);

Thanks for checking out the SDK and I hope this helps!

Dan Nesfeder
  • 298
  • 1
  • 6
  • Okay i'll try this but if i set this (setCameraMode) zoom ( CameraPosition position = new CameraPosition.Builder() .target(new LatLng(location.getLatitude(), location.getLongitude())) .zoom(15)) zoom(first time zoom) is not working , i removed setCameraMode zoom is working – Netset Android Oct 31 '18 at 13:05
  • Hey @Dan Nesfeder Please reply – Netset Android Nov 01 '18 at 11:40
  • @NetsetAndroid Yeah you need to use `zoomWhileTracking` to adjust the map zoom after the `LocationComponent` has been initialized. What are you trying to do with `CameraPosition`? Set the initial position of the map? – Dan Nesfeder Nov 01 '18 at 13:08
  • CameraPosition initial on "onLocationChanged(Location location)" zoomWhileTracking is not working, check above my code already set this. In latest lib 0.22.0 navigation is not working on Samsung Galaxy Note 9 – Netset Android Nov 02 '18 at 05:12