问题
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.
回答1:
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);
}
回答2:
Calculate both bearings using an API you have. Then write a function angleDiff(angle1, angle2).
There are two possible angleDiff types: One that retunrs negative angles, too. And one that only delivers positive angles.
Test that function with these test cases:
angleDiff(350, 10): diff = 20
10, 350: diff = 20 (or -20)
170, 190 :diff = 20
190, 170: diff 20 (or -20)
0,360 and 360,0: diff = 0
来源:https://stackoverflow.com/questions/24045510/how-to-calculate-angle-between-two-path-lines-on-google-map-using-lat-long-coord