我要求提供有关我如何实现此功能的更多详细信息。你可以在这里看到一个例子: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 文件并构建一组坐标,然后可以将其添加到多边形的路径:属性。不幸的是,您似乎无法仅使用 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 图层转换为多边形需要绕道而行。