Javascript 谷歌地图 - 如何以米为单位获得两点之间的距离?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7997627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:19:26  来源:igfitidea点击:

Google Maps - How to get the distance between two point in metre?

javascriptgoogle-mapsgoogle-maps-api-3

提问by markzzz

I have these coordinate :

我有这些坐标:

(45.463688, 9.18814) 
(46.0438317, 9.75936230000002)

and I need (trought Google API V3, I think) to get the distance between those 2 points in metre. How can I do it?

我需要(我认为是 Google API V3)以米为单位获得这两个点之间的距离。我该怎么做?

回答by Roman Goyenko

If you're looking to use the v3 google maps API, here is a function to use: Note: you must add &libraries=geometryto your script source

如果您想使用 v3 谷歌地图 API,这里有一个可以使用的函数:注意:您必须添加&libraries=geometry到您的脚本源中

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>

<script>
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);

alert(calcDistance(p1, p2));

//calculates distance between two points in km's
function calcDistance(p1, p2) {
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}

</script>

回答by PiRX

I think you could do without any specific API, and calculate distance with plain Javascript:

我认为您可以在没有任何特定 API 的情况下进行,并使用纯 Javascript 计算距离:

Thissite has good info about geographical calculations and Javascript sample for distance calculation.

这个站点有关于地理计算的很好的信息和用于距离计算的 Javascript 示例。

Ok, quick glance at Google API page and it seems, you could do it by:

好的,快速浏览一下 Google API 页面,您似乎可以通过以下方式完成:

  1. Call DirectionsService().route() to get DirectionsResultwith routes
  2. For one or each route go through its legs property and calculate sum of distances
  1. 调用 DirectionsService().route() 以获取带有路线的DirectionsResult
  2. 对于一条或每条路线,通过它的legs属性并计算距离总和

回答by Pixelmixer

This is primarily done with math outside of the google api, so you shouldn't need to use the API for anything other than finding the correct coordinates.

这主要是通过 google api 之外的数学完成的,因此除了找到正确的坐标之外,您不需要使用该 API 进行任何其他操作。

Knowing that, here's a link to a previously answered question relating to Javascript.

知道这一点,这里有一个链接,指向先前回答的与 Javascript 相关的问题。

Calculate distance between 2 GPS coordinates

计算 2 个 GPS 坐标之间的距离

回答by Rob

http://www.csgnetwork.com/gpsdistcalc.html

http://www.csgnetwork.com/gpsdistcalc.html

Has nothing to do with coding btw

与编码无关 btw

EDIT if you're looking for some code

如果您正在寻找一些代码,请编辑

Calculate distance between two points in google maps V3

计算谷歌地图V3中两点之间的距离

回答by Tommaso

Try this

尝试这个

 CLLocationCoordinate2D coord1;
 coord1.latitude = 45.463688;
 coord1.longitude = 9.18814;
 CLLocation *loc1  = [[[CLLocation alloc] initWithLatitude:coord1.latitude longitude:coord1.longitude] autorelease];

 CLLocationCoordinate2D coord2;
 coord2.latitude = 46.0438317;
 coord2.longitude = 9.75936230000002;
 CLLocation *loc2  = [[[CLLocation alloc] initWithLatitude:46.0438317 longitude:9.75936230000002] autorelease];

 CLLocationDistance d1 = [loc1 distanceFromLocation:loc2];

回答by Nguyen Chi Thanh

Try this:

尝试这个:

const const toRadians = (val) => {
    return val * Math.PI / 180;
}
const toDegrees = (val) => {
    return val * 180 / Math.PI;
}
// Calculate a point winthin a circle
// circle ={center:LatLong, radius: number} // in metres
const pointInsideCircle = (point, circle) => {
    let center = circle.center;
    let distance = distanceBetween(point, center);

    //alert(distance);
    return distance < circle.radius;
};

const distanceBetween = (point1, point2) => {
   //debugger;
   var R = 6371e3; // metres
   var φ1 = toRadians(point1.latitude);
   var φ2 = toRadians(point2.latitude);
   var Δφ = toRadians(point2.latitude - point1.latitude);
   var Δλ = toRadians(point2.longitude - point1.longitude);

   var a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
           Math.cos(φ1) * Math.cos(φ2) *
           Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

   return R * c;
}

References: http://www.movable-type.co.uk/scripts/latlong.html

参考资料:http: //www.movable-type.co.uk/scripts/latlong.html