これをどのように実装したかについて、より詳細な情報を提供してほしいというリクエストがありました。ここで例を見ることができます: http://www.usinternet.com/fiber-info/map.php
マップ上にいくつかのマーカーがあり、それらが境界の内側にあるか外側にあるかをテストしたいと考えています。私はそれらを次のように設定しました:
var userLocation = new google.maps.LatLng(44.928633,-93.298919); //Lyndale Park
var wendysLocation = new google.maps.LatLng(44.948056,-93.282222); //Wendy's on Lake St
var userMarker = new google.maps.Marker({
position: userLocation,
map: map,
title:"Your Location",
draggable: true
});
var wendysMarker = new google.maps.Marker({
position: wendysLocation,
map: map,
title:"Wendy's on Lake St",
draggable: true
});
ポリゴンを作成するには、KML ファイルを読み取って一連の座標を作成する必要があります。これをポリゴンの paths: プロパティに追加できます。残念ながら、Google Maps API だけを使用して KML から Polygon に移行することはできないようですが、間違っていたら誰か訂正してください!
var sector3PillburyPleasant = new google.maps.KmlLayer('http://usinternet.com/fiber-info/kml/Sector3Pillbury-Pleasant.kml', kmlOptions);
sector3PillburyPleasant.setMap(map);
var coordsSector3PillburyPleasant = [
new google.maps.LatLng(44.91615269014508, -93.28382953316716),
new google.maps.LatLng(44.91619137463104, -93.28120338511586),
new google.maps.LatLng(44.93046751403393, -93.28114057929436),
new google.maps.LatLng(44.93046991974436, -93.28055243604703),
new google.maps.LatLng(44.94815274527994, -93.28053962401711),
new google.maps.LatLng(44.94815856171297, -93.28364017122617),
];
var polySector3PillburyPleasant = new google.maps.Polygon({
paths: coordsSector3PillburyPleasant,
strokeColor: "#FFFFFF",
strokeOpacity: 0.0,
fillColor: "#FFFFFF",
fillOpacity: 0.0
});
KML からポリゴンのパスを解析したところ、パスを構成する座標は次のようにネストされていました (パスを閉じている場合、最後の座標ペアは必要ありません)。
<kml>
<Document>
<Placemark>
<Polygon>
<tessellate>1</tessellate>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-93.28382953316716,44.91615269014508,0 -93.28120338511586,44.91619137463104,0 -93.28114057929436,44.93046751403393,0 -93.28055243604703,44.93046991974436,0 -93.28053962401711,44.94815274527994,0 -93.28364017122617,44.94815856171297,0 -93.28382953316716,44.91615269014508,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
次に、取得したマーカーがポリゴンの内側にあるか外側にあるかをテストします。
var markerIn = google.maps.geometry.poly.containsLocation(wendysMarker.getPosition(), polySector3PillburyPleasant);
var markerOut = google.maps.geometry.poly.containsLocation(userMarker.getPosition(), polySector3PillburyPleasant);
console.log(markerIn);
console.log(markerOut);
次に、KML レイヤーをポリゴンに変換するために少し回り道がありましたが、KML レイヤーの内側または外側にいるかどうかの真偽テストを行います。