如何通过Google maps v3中的几何库指示某个点位于圆形或矩形内?

时间:2014-03-12 06:19:07

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

看看这个:

if(google.maps.geometry.poly.containsLocation(new google.maps.LatLng(arrayLatitude[counter1], arrayLongitude[counter1]), polygon))
{
}

这是表示点在多边形内部或不在的条件。 我正在寻找类似但是圆形或矩形的东西。

类似的东西:

if(google.maps.geometry.circle.containsLocation(new google.maps.LatLng(arrayLatitude[counter1], arrayLongitude[counter1]), circle)
{
}

或矩形:

if(google.maps.geometry.rectangle.containsLocation(new google.maps.LatLng(arrayLatitude[counter1], arrayLongitude[counter1]), rectangle)
{
}

2 个答案:

答案 0 :(得分:6)

CircleRectangle都有方法

  

getBounds()LatLngBounds返回此矩形的边界。

尝试调用方法getBounds()返回google.maps.LatLngBounds项。它有方法

  

contains(latLng:LatLng)boolean如果给定的lat / lng在此边界内,则返回true。

类似

(new google.maps.Circle({center: new google.maps.LatLng(YOUR_LAT, YOUR_LNG),radius: YOUR_RADIUS})).getBounds().contains(google.maps.LatLng(POINT_LAT, POINT_LNG))

希望这是你需要的

答案 1 :(得分:0)

重要提示:如果您没有设置getSouthWest和getNorth ......它总是返回false: Google Maps v3 map.getBounds().containsLatLng is not a function

var temp_rectangle = new google.maps.Rectangle({
    bounds: new google.maps.LatLngBounds(
    new google.maps.LatLng(array_shapes_object[counter].getSouthWestLat, array_shapes_object[counter].getSouthWestLng),
    new google.maps.LatLng(array_shapes_object[counter].getNorthEastLat, array_shapes_object[counter].getNorthEastLng))
});

if(temp_rectangle.getBounds().contains(new google.maps.LatLng(arrayLatitude[counter1], arrayLongitude[counter1])))
{
    alert("Inside");
}
else
{
    alert("Outside");
}
相关问题