【问题标题】:Google Maps - How to get the distance between two point in metre?谷歌地图 - 如何以米为单位获得两点之间的距离?
【发布时间】:2011-12-21 07:25:06
【问题描述】:

我有这些坐标:

(45.463688, 9.18814) 
(46.0438317, 9.75936230000002)

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

【问题讨论】:

标签: javascript google-maps google-maps-api-3


【解决方案1】:

如果您希望使用 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>

【讨论】:

  • 我添加了用法示例,您需要传递 2 个坐标。它对我有用,返回 78.35
  • 请不要忘记将问题标记为已回答,以帮助将来的人们找到正确的答案。
  • 返回值是英里还是公里?
  • 正如评论所说://计算两点之间的距离,单位为公里
  • console.log(calcDistance("45.463688, 9.18814", "46.0438317, 9.75936230000002")); 它返回 NaN,因为我们向 calDistance 提供字符串。试着让它console.log(calcDistance(45.463688, 9.18814, 46.0438317, 9.75936230000002));
【解决方案2】:

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

This 网站有关于地理计算和距离计算的 Javascript 示例的详细信息。

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

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

【讨论】:

  • 感谢您提供第一个链接。可以使用近似公式优化近距离的计算
【解决方案3】:

【讨论】:

    【解决方案4】:

    这主要是通过 google api 之外的数学运算完成的,因此除了查找正确的坐标之外,您不需要使用该 API。

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

    Calculate distance between 2 GPS coordinates

    【讨论】:

      【解决方案5】:

      试试这个

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

      【讨论】:

      • 如何获得2点之间的路线距离,这只是给出了直接距离(在物理文献中:位移),如何获得路径距离
      • 这是目标 C :) 问题是关于谷歌地图的 Javascript API
      • 请提供解释而不是“试试这个”。
      【解决方案6】:

      试试这个:

      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

      【讨论】:

      • 请改进您的答案。为什么它更好,它是如何工作的,等等。“试试这个”是不够的。 Stackoverflow 不是盲目地尝试事物,而是阅读和理解事物是如何工作的。仍然感谢您的贡献:)
      【解决方案7】:

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

      【讨论】:

        【解决方案8】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-29
          • 1970-01-01
          • 1970-01-01
          • 2014-07-12
          • 2012-05-13
          • 2017-08-22
          • 2017-04-17
          • 1970-01-01
          相关资源
          最近更新 更多