[英]How do I calculate the distance between two points?
I have a calculate function and the latlng values but how do I calculate the distance between the first and the last? 我有一个计算函数和latlng值,但我如何计算第一个和最后一个之间的距离?
function calculate() {
//poo abbreviation for point of origin not poo=shit
var poolat = locations[0].lat;
var poolng = locations[0].lng;
var destlat = locations[locations.length - 1].lat;
var destlng = locations[locations.length - 1].lng;
This question has already been answered here: Calculate distance between two latitude-longitude points? 这个问题已在这里得到解答: 计算两个纬度 - 经度点之间的距离? (Haversine formula)
(Haversine配方)
Note that the Haversine formula assumes a perfect sphere, not a spheroid which is what the earth is. 请注意,Haversine公式假设一个完美的球体,而不是一个球体是地球的球体。
There is two ways, you can use the Haversine formula which requires a little more typing or you can use the API. 有两种方法,你可以使用Haversine公式,它需要更多的输入或你可以使用API。
Try the following in your calculate function 在计算功能中尝试以下操作
var distance = google.maps.geometry.spherical.computeDistanceBetween(
new google.maps.LatLng(poolat, poolng),
new google.maps.LatLng(destlat, destlng)
);
console.log(distance); 的console.log(距离);
Note: 注意:
The default value of the distance is in metres. 距离的默认值以米为单位。
Try this. 试试这个。 The built in Math functions make it much easier
内置的Math函数使其更容易
function calculate() {
//poo abbreviation for shit
var poolat = locations[0].lat;
var poolng = locations[0].lng;
var destlat = locations[locations.length - 1].lat;
var destlng = locations[locations.length - 1].lng;
return Math.sqrt(Math.pow((destlat - poolat),2) + Math.pow((destlng - poolng),2))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.