2011-09-01 93 views
1

我需要确定某些LatLngs是否位于Google地图圈内(其中一个为http://code.google.com/apis/maps/documentation/javascript/overlays.html#Circles)。我该如何解决这个问题?我制作圈子的标记是:计算LatLng与LatLng之间的距离(或圆圈中的点数) - Google Maps v3

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

我可以使用半径吗?

回答

2
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

+0

顺便说一句,你**不能**使用毕达哥拉斯定理,因为地球是不平坦的! – kargeor

+0

在网站上的第一个很好的答案,非常感谢,绝对是最好的解决方案和我正在寻找的。你知道这个选项是否有任何请求限制? – rickyduck

+1

该文档没有提到任何限制。我不确定该函数是在本地还是在服务器上进行评估。 – kargeor

1

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

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

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

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 
1

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

X = X1 - X2 
Y = Y1 - Y2 

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

从1至2 =开方距离(X^2 + Y^2)

然后你就可以比较你的半径。如果距离小于半径,则该点位于圆内。

您需要首先获取圆的中心点和试图比较的点。这些必须在相同的坐标空间中。

相关问题