Batuhan Coşkun
Batuhan Coşkun

Reputation: 2969

How to relocate MyLocationView in Mapbox Android SDK?

I want to change the location of MyLocationView in MapView of Mapbox. I don't want to use a custom marker. Because calculating bearing and adding this to a marker (like in navigation apps) is difficult for me(Sensor Fusion & GPS bearing should be done) and also MyLocationView is smoother than a marker in relocating and changing bearing operations. I'm continuing with Mapbox MyLocationView. I have done bearing tracking integration and showing location on the MapView. What i have done is:

mapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(MapboxMap mapboxMap) {
        map = mapboxMap;

        map.setMyLocationEnabled(true);
        map.getTrackingSettings().setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW);
        map.getTrackingSettings().setMyBearingTrackingMode(MyBearingTracking.COMPASS);
        map.getTrackingSettings().setDismissBearingTrackingOnGesture(false);
        map.getMyLocationViewSettings().setForegroundDrawable(iconDrawable, iconDrawable);
        map.getMyLocationViewSettings().setBackgroundTintColor(Color.parseColor("#56B881"));
        map.getMyLocationViewSettings().setForegroundTintColor(Color.parseColor("#FFFFFF"));
        map.getMyLocationViewSettings().setAccuracyTintColor(Color.parseColor("#56B881"));
    }
});

My View: enter image description here

So i want to ask is it possible to update MyLocationView's Location in Mapbox Android SDK? I do lots of search but i can not figure out that. Maybe there is a trick about this.

Upvotes: 0

Views: 676

Answers (2)

I use listener and it works well

mapboxMap.setOnMyLocationChangeListener(myLocationChangeListener);
mapboxMap.setMyLocationEnabled(true);

private MapboxMap.OnMyLocationChangeListener myLocationChangeListener = new MapboxMap.OnMyLocationChangeListener() {

    @Override
    public void onMyLocationChange(@Nullable Location location) {

    }
};

after

 myLocationChangeListener.onMyLocationChange(location);

Upvotes: 0

Eugene P.
Eugene P.

Reputation: 1036

Firstly, you should implement ProgressChangeListener in your Activity from this package com.mapbox.services.android.navigation.v5.listeners.ProgressChangeListener
After that in the next overrided method you can update your location

@Override
public void onProgressChange(Location location, RouteProgress 
routeProgress) {
    Log.e(TAG, "onProgressChange" + location);
    Log.d(TAG, "onProgressChange: fraction of route traveled: %f" 
    + routeProgress.getFractionTraveled());
    if(mapboxMap.getMyLocation()!=null){  
         mapboxMap.getMyLocation().setLatitude(location.getLatitude());       
         mapboxMap.getMyLocation().setLongitude(location.getLongitude());
    }
}

And for userOffRoute method from implemented OffRouteListener you can do the same.

Also I'd like to notice that in build.gradle I have next dependencies

compile('com.mapbox.mapboxsdk:mapbox-android-sdk:5.1.0@aar') {
    transitive = true
}
compile 'com.mapbox.mapboxsdk:mapbox-android-ui:2.1.3'
compile 'com.mapbox.mapboxsdk:mapbox-android-services:2.1.3'
compile 'com.mapbox.mapboxsdk:mapbox-android-telemetry:2.1.3'
compile 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.3.1'
compile 'com.mapbox.mapboxsdk:mapbox-java-core:2.1.3'
compile 'com.mapbox.mapboxsdk:mapbox-java-geojson:2.1.3'
compile 'com.mapbox.mapboxsdk:mapbox-java-services:2.1.3'
compile 'com.mapbox.mapboxsdk:mapbox-java-services-rx:2.1.3'

Upvotes: 1

Related Questions