谷歌地图-如何得到两点之间的距离在米

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

本文关键字:之间 两点 距离 何得 谷歌地图      更新时间:2023-09-26

有这些坐标:

(45.463688, 9.18814) 
(46.0438317, 9.75936230000002)

和我需要(通过谷歌API V3,我认为)得到这两个点之间的距离在米。我该怎么做呢?

如果你想使用v3 google maps 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>

我认为你可以不使用任何特定的API,并使用普通Javascript计算距离:

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

好,快速浏览一下Google API页面,你可以这样做:

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

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

与编码无关btw

编辑如果你正在寻找一些代码

计算google maps V3中两点之间的距离

这主要是通过google api之外的数学来完成的,所以除了找到正确的坐标之外,你不需要使用api来做任何事情。

知道了这些,这里有一个链接到之前回答的关于Javascript的问题。

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

试试这个

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

试试这个:

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

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

您指的是整个路径的长度,还是只想知道位移(直线距离)?我看这里没有人指出距离和位移之间的区别。对于距离计算由JSON/XML数据给出的每个路线点,对于位移有内置的Spherical类解决方案。

您指的是整个路径的长度,还是只想知道位移(直线距离)?我看这里没有人指出距离和位移之间的区别。对于距离,计算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);
}