2013-02-26 224 views
4

裏面我的LatLng有一個典型的多邊形谷歌地圖與一些的latLng的設定:是多邊形

var bermudaTriangle = new google.maps.Polygon({ 
    paths: [ 
    new google.maps.LatLng(25.774252, -80.190262), 
    new google.maps.LatLng(18.466465, -66.118292), 
    new google.maps.LatLng(32.321384, -64.75737), 
    ] 
}); 
bermudaTriangle.setMap(map); 

這些都是谷歌的文檔中的百慕大三角COORDS。我也有一些隨機座標,saaay:

var coord1 = new new google.maps.LatLng(26.194876675795218,-69.8291015625) 
var coord2 = new new google.maps.LatLng(33.194876675795218,-63.8291015625) 

第一個恰好在三角形內,第二個在外面。這些只是一個例子,我需要的是一種方法來找出,如果一個提供的座標(不只是這兩個座標中的一個)在多邊形的內部或外部(也不總是百慕大三角形,甚至不是三角形 - 多邊形可以有任意數量的latLng)。我瀏覽了Google Maps API文檔,但找不到任何提供 這個問題的答案。

回答

8

使用containsLocation(point:LatLng, polygon:Polygon)方法。

containsLocation(點:經緯度,多邊形:多邊形)布爾計算給定的點是否位於指定的多邊形內。

proof of concept fiddle

代碼片斷:

function checkInPolygon(marker, polygon) { 
 
    var infowindow = new google.maps.InfoWindow(); 
 
    var html = ""; 
 
    if (google.maps.geometry.poly.containsLocation(marker.getPosition(), polygon)) { 
 
    html = "inside polygon"; 
 
    } else { 
 
    html = "outside polygon"; 
 
    } 
 
    infowindow.setContent(html); 
 
    infowindow.open(map, marker); 
 
} 
 

 
var map; 
 
var coord1 = new google.maps.LatLng(26.194876675795218, -69.8291015625); 
 
var coord2 = new google.maps.LatLng(33.194876675795218, -63.8291015625); 
 

 
function initialize() { 
 
    var map = new google.maps.Map(
 
    document.getElementById("map_canvas"), { 
 
     center: new google.maps.LatLng(37.4419, -122.1419), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    bermudaTriangle.setMap(map); 
 
    var bounds = new google.maps.LatLngBounds(); 
 
    for (var i = 0; i < bermudaTriangle.getPath().getLength(); i++) { 
 
    bounds.extend(bermudaTriangle.getPath().getAt(i)); 
 
    } 
 
    bounds.extend(coord1); 
 
    bounds.extend(coord2); 
 
    var marker1 = new google.maps.Marker({ 
 
    map: map, 
 
    position: coord1 
 
    }); 
 
    var marker2 = new google.maps.Marker({ 
 
    map: map, 
 
    position: coord2, 
 
    }); 
 
    map.setCenter(bounds.getCenter()); 
 
    map.setZoom(3); 
 
    checkInPolygon(marker1, bermudaTriangle); 
 
    checkInPolygon(marker2, bermudaTriangle); 
 
} 
 
google.maps.event.addDomListener(window, "load", initialize); 
 

 
var bermudaTriangle = new google.maps.Polygon({ 
 
    paths: [ 
 
    new google.maps.LatLng(25.774252, -80.190262), 
 
    new google.maps.LatLng(18.466465, -66.118292), 
 
    new google.maps.LatLng(32.321384, -64.75737), 
 
    ] 
 
});
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script> 
 
<div id="map_canvas"></div>

1

如果Google Maps API沒有專門的方法,那麼您需要查找或編寫自己的方法。這是計算機圖形學中的一個常見問題,如果您使用Google「多邊形點擊測試」,您會發現大量信息。 This SO answer看起來相當全面。

0

看看維基百科Point in polygon page。有幾種方法可以確定點是否在多邊形內。其中之一是光線投射算法。 This page有一些關於如何在谷歌地圖中實現的例子。