'How to find the degree of rotation for marker on map while using react-native-maps with google maps api?
I am trying to make an uber like app using react-native. I am using react-native-maps. I have successfully mapped the directions using directions api of google maps, and I have been able to move my car marker on the path successfully, but the problem is the direction of the car. I want to change the direction of the car as the direction of the road changes. I have been trying to find a way to do it for a long time, but I have found nothing. If anyone can help, that will be much appreciated.
Solution 1:[1]
You can find the the degree of rotation between two coordinates by using the following function
const calculateHeading = (cord1, cord2) => {
if (cord2) {
const {latitude: lat1, longitude: lng1} = cord1;
const {latitude: lat2, longitude: lng2} = cord2;
const y = Math.sin(lng2 - lng1) * Math.cos(lat2);
const x =
Math.cos(lat1) * Math.sin(lat2) -
Math.sin(lat1) * Math.cos(lat2) * Math.cos(lng2 - lng1);
const ? = Math.atan2(y, x);
const brng = ((? * 180) / Math.PI + 360) % 360;
return brng;
}
return 0;
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Abubakar Waris |