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

42
我有这些坐标:
(45.463688, 9.18814) 
(46.0438317, 9.75936230000002)

我需要通过Google API V3(我想)获取这2个点之间的距离,单位为米。我该怎么做?


1
如果您需要计算距离和旅行时间,可以使用距离矩阵API。 - Surjeet Rajput
计算Google Maps V3中两点之间的距离 - TarangP
8个回答

125
如果你想使用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>

3
请不要忘记标记问题为已回答,以帮助未来的人们找到正确的答案。 - Roman Goyenko
1
这会返回英里还是公里的值? - Zabs
4
正如该评论所述://计算两点之间的距离(单位为公里) - Roman Goyenko
console.log(calcDistance("45.463688, 9.18814", "46.0438317, 9.75936230000002")); 这段代码返回 NaN,因为我们向 calcDistance 函数提供了字符串。请尝试修改为 console.log(calcDistance(45.463688, 9.18814, 46.0438317, 9.75936230000002)); - Nishant Nawarkhede
如果您需要计算距离和旅行时间,可以使用距离矩阵API。请注意,此API仅供参考,具体结果可能会因实际情况而异。 - Surjeet Rajput
显示剩余4条评论

4

我认为您可以不使用任何特定的API,而是使用纯JavaScript计算距离:

这个网站提供了有关地理计算和JavaScript距离计算示例的良好信息。

好的,快速查看Google API页面,似乎您可以通过以下方式实现:

  1. 调用DirectionsService().route()以获取带路线的DirectionsResult
  2. 对于每条路线或其中一条路线,遍历其legs属性并计算距离总和

感谢您提供的第一个链接。使用近似公式可以大大优化计算接近距离的过程。 - Dan

1

试试这个

 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];

1
如何获取两点之间的路线距离,这只给出了直接距离(在物理文献中称为位移),如何获取路径距离。 - Mehul Thakkar
这是关于Google Maps的Javascript API的问题,而不是Objective C :) - Martijn Scheffer
请提供解释,而不是简单地说“试一下”。 - Tõnu Samuel

1

1

这主要是通过数学计算完成的,与谷歌API无关,因此您不应该需要使用API来执行除了查找正确坐标之外的任何操作。

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

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


0

试试这个:

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.cos1) * Math.cos2) *
           Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

   return R * c;
}

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


1
请改进您的回答。为什么它更好,它是如何工作的等等。"试试这个"不够。Stackoverflow不是盲目尝试事物,而是阅读和理解事物如何工作。仍然感谢您的贡献 :) - Tõnu Samuel

0
你是指路径的长度还是只想知道位移(直线距离)?我看这里没有人指出距离和位移的区别。对于距离,计算JSON/XML数据给出的每个路由点,对于位移,则可以使用内置解决方案使用 Spherical 类。如果你指的是距离,那么你只需在每个路径点上使用 computeDistanceBetween 并连接即可。
//calculates distance between two points in km's
function calcDistance(p1, p2) {
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}

0
你是指整个路径的长度还是只想知道位移(直线距离)?我看没有人指出距离和位移之间的区别。对于距离,可以根据JSON/XML数据计算每个路由点,而对于位移,则可以使用内置的Spherical类解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接