Search code examples
iosgoogle-mapsgeolocationgisgoogle-maps-sdk-ios

how to calculate angle between two path lines on google map using lat-long coordinates


I want to calculate angle between two path lines on Google map. I have the lat-long coordinates of the end points of the lines. Please suggest If there is other information I can use to do this that is available from Google maps.


Solution

  • If the distances are small you can use this method. The accuracy reduces with large distances and the further you move from the equator. First find bearings with computeHeading()

    computeHeading(from:LatLng, to:LatLng) .Returns the heading from one LatLng to another LatLng. Headings are expressed in degrees clockwise from North within the range [-180,180).

    function getBearings(){
        var spherical = google.maps.geometry.spherical; 
        var point1 = markers[0].getPosition();// latlng of point1
        var point2 = markers[1].getPosition();
        var point3 = markers[2].getPosition();
        var bearing1 = google.maps.geometry.spherical.computeHeading(point1,point2);
        var bearing2 = google.maps.geometry.spherical.computeHeading(point2,point3);
        var angle =getDifference(bearing1, bearing2);
        return angle;
    }
    

    You can then use this function to calculate angle between the bearings.

    function getDifference(a1, a2) {
        al = (a1>0) ? a1 : 360+a1;
        a2 = (a2>0) ? a2 : 360+a2;
        var angle = Math.abs(a1-a2)+180;
        if (angle > 180){
            angle = 360 - angle;
        }
       return   Math.abs(angle);
    }