【问题标题】:Work out LatLng distance from LatLng (or point in circle) - Google Maps v3计算 LatLng 与 LatLng 的距离(或圆点) - Google Maps v3
【发布时间】:2011-09-01 13:16:30
【问题描述】:

我需要确定某些 LatLng 是否在 Google 地图圈内(其中之一:http://code.google.com/apis/maps/documentation/javascript/overlays.html#Circles)。我将如何解决这个问题?我制作圆圈的标记是:

geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        circlemarker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
        THEradius = parseFloat(THEradius);
         var populationOptions = {
          strokeColor: "#BDAEBB",
          strokeOpacity: 0.8,
          strokeWeight: 2,
          fillColor: "#BDAEBB",
          fillOpacity: 0.5,
          map: map,
          center: results[0].geometry.location,
          radius: THEradius
         };
        cityCircle = new google.maps.Circle(populationOptions);
        map.fitBounds(cityCircle.getBounds());
    }
});

我可以只使用半径吗?

【问题讨论】:

    标签: javascript google-maps-api-3 point-in-polygon


    【解决方案1】:
    var distance = google.maps.geometry.spherical.computeDistanceBetween(
           results[0].geometry.location, otherLatLng);
    
    if (distance <= THEradius) {...} else {...}
    

    我希望这对你有用。见http://code.google.com/apis/maps/documentation/javascript/reference.html#spherical

    【讨论】:

    • 顺便说一句,你不能使用勾股定理,因为地球不是平的!
    • 网站上的第一个答案很好,非常感谢,绝对是最好的解决方案,也是我正在寻找的解决方案。您知道此选项是否有任何请求限制?
    • 文档没有提到任何限制。我不确定该函数是在本地还是在服务器上评估的。
    【解决方案2】:

    您需要做的是将您的经纬度列表转换为谷歌坐标空间,或将圆圈转换为经纬度坐标空间。

    如何进行转换取决于您使用的语言,但如果是一次性的,有些网站会为您进行转换。

    一旦您在与圆相同的坐标空间中获得了经纬度位置,您就可以使用简单的毕达哥拉斯数学计算出该位置是否小于圆的半径(如您所建议的那样)。

    HYP = (OPP^2 * ADJ^2)^0.5
    

    地点:

    OPP is the difference in x direction from the centre of the circle
    ADJ is the difference in y direction from the centre of the circle.
    HYP is the distance in a straight line from the centre of the circle
    

    【讨论】:

      【解决方案3】:

      在数学方面,要在 2D 中找到一个点到另一个点的距离,请使用Pythagoras

      X = X1 - X2
      Y = Y1 - Y2
      

      (上面有效地计算了从一个点到另一个点的向量)

      1 到 2 的距离 = sqrt(X^2 + Y^2)

      然后您可以将其与您的半径进行比较。如果距离小于您的半径,则该点在圆内。

      您需要首先获取与圆心相对应的点和您要比较的点。它们必须在同一个坐标空间中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多