Search code examples
javaandroidmapbox-androidmapbox-marker

Rotate marker(arrow) as per user navigation on mapbox maps android


I am working on an application in which I have to rotate an arrow according to the user navigation.

I am using the latest Mapbox SDK.

I have tried with a barrier (calculating using lat & long) but couldn't get success.

I don't have any clue that how can I achieve it.

It(Arrow marker) should be navigated on a predefined polygon path.

enter image description here


Solution

  • I got success in calculate bearing using the below method and rotate the arrow as per user navigation:

    float bearingBetweenLocations(LatLng latLng1, LatLng latLng2) {
            double PI = 3.14159;
            double lat1 = latLng1.getLatitude() * PI / 180;
            double long1 = latLng1.getLongitude() * PI / 180;
            double lat2 = latLng2.getLatitude() * PI / 180;
            double long2 = latLng2.getLongitude() * PI / 180;
            double dLon = (long2 - long1);
            double y = Math.sin(dLon) * Math.cos(lat2);
            double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
                    * Math.cos(lat2) * Math.cos(dLon);
            double brng = Math.atan2(y, x);
            brng = Math.toDegrees(brng);
            brng = (brng + 360) % 360;
    
            return (float) brng;
        }
    

    Note: Arrow's head should be pointed towards the up direction like the below image. If it is not then you have to set bearing +/- as per the arrow's direction.

    enter image description here