简体   繁体   English

如何计算两点之间的距离?

[英]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.

相关问题 如何计算由纬度和经度和海拔高度指定的两点(垂直)之间的距离? - How do I calculate the distance between two points (vertically) specified by latitude and longitude and Altitude? 如何计算两点之间的角度? - How do I calculate the angle between two points? 如何在 React Native 中导入 geolib 来计算两点之间的距离? - How to import geolib in React Native to calculate the distance between two points? 如何计算路径上两点之间的距离? - How to calculate a distance between two points along a path? OpenLayers 6:如何从 api 计算两点(可点击)之间的距离? - OpenLayers 6: How to calculate the distance between two points (clickable) from api? 如何使用dist()函数计算5点之间的距离? - how can i calculate the distance between 5 points using the dist() function? OpenLayers 3:如何计算2点之间的距离? - OpenLayers 3: How to calculate distance between 2 points? 如何使用坐标计算两个点的距离? - How to calculate the distance of two points using coordinates? 将滚动值作为百分比传递给参数,计算两点之间的距离? - Pass scroll value as percentage as argument, calculate distance between two points? 根据左距离计算两点之间的当前位置 - Calculate current position between two points based on distance left
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM