5

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
Kutbi
  • 1,154
  • 2
  • 18
  • 32

3 Answers3

2

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

Kutbi
  • 1,154
  • 2
  • 18
  • 32
0

1- first you need the input - you need to get the getBearing() from the location object.

2- you need the correct output - you can do it in 2 ways -

a. if you use png icons , you can create an arrow for every 10 degrees, (36 arrows) and updated the marker with correct icon, if current bearing is different from the old bearing.

b. Better option but i don't know if this works: the marker of mapbox has options.rotation option. But in android, Mapbx moved to Mapbox annotation plugin, see also here

the SymbolManager has a function setIconRotationAlignment. so if you can use this class, i believe you can set the rotation of the icon by the bearing.

you can also see in this demo that one of the options is "set icon rotation to 45"

enter image description here

3- One more thing - you may want to get also input of getBearingAccuracyDegrees to know when you don't have a good enough Accuracy of the Bearing, and give indication to the user.

Citizen-Dror
  • 843
  • 9
  • 17
0

If you prefer to use a library instead of the method you suggested then I suggest to use Turf . "Turf android" is a library created by the mapbox guys ,which have many useful methods including this one .

/*
Takes two {@link Point}s and finds the geographic bearing between them.
*/
TurfMeasurement.bearing(@NonNull Point point1, @NonNull Point point2) 

library homepage : https://docs.mapbox.com/android/java/guides/turf/

A.Alqadomi
  • 1,529
  • 3
  • 25
  • 33