Google Maps Javascript - computeHeading and compass heading to calculate heading

By using the computeHeading () function against my currentPosition and destinationPosition, I can get the angle returned (tis is currently between -180 and +180).

heading = google.maps.geometry.spherical.computeHeading(
    currentLocation,
    destinationLocation
);

      

I can get the direction of the compass using the function to return alpha, which gives me the angle of rotation from north.

alpha = null;
//Check for iOS property
if (event.webkitCompassHeading) {
    //window.confirm("iOS device - using webKit instead"); // report back that we are indeed on iOS
    alpha = event.webkitCompassHeading;
}
//non iOS
else {
    alpha = event.alpha;
}

var locationIcon = myLocationMarker.get('icon');
locationIcon.rotation = 360 - alpha;
myLocationMarker.set('icon', locationIcon);

      

This gives me the angle and then helps me rotate the icon so I can see if it is pointing the correct path correctly

Can someone tell me the math / js code to get the way I am facing the assignment to give me the returned result. I need to know if I come across a destination and then I see that the path does not match, etc.

I'm going to try and use some web audio panning to help people point the way correctly.

thank

edit: here is an image, maybe help clarify. I am sure this is a simple calculation, but I cannot figure it outenter image description here

+5


source to share


2 answers


Calculate the angle between two latitude and longitude points

private double angleFromCoordinate(double lat1, double long1, double lat2,
        double long2) {

    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;
    brng = 360 - brng; // count degrees counter-clockwise - remove to make clockwise

    return brng;
}

      

================================================== ====================



Using GoogleMap API:

<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=geometry"></script>

var point1 = new google.maps.LatLng(lat1, lng1);
var point2 = new google.maps.LatLng(lat2, lng2);
var heading = google.maps.geometry.spherical.computeHeading(point1,point2);

      

+2


source


I think this is just math because ..

diff(Lat)/diff(lng) = tan(alpha)

      



then i think you can figure out the rest?

note that since the title may change radically due to GPS accuracy problem. Consider using compass data and some algorithms to make outliners easier.

0


source







All Articles