【发布时间】:2016-11-12 11:58:20
【问题描述】:
【问题讨论】:
-
@xomena 非常感谢你,你真的帮了我
标签: google-maps dictionary polygon
【问题讨论】:
标签: google-maps dictionary polygon
您可以使用包含至少 3 个元素的 lat, lng 对的数组来构建多边形。
如果数组没有关闭,谷歌地图会默认关闭它。
虽然对于大多数 DBMS,您必须保存一个封闭的坐标数组。关闭是什么意思?本质上,数组的第一个点和最后一个点是相等的。 DBMS 与 MongoDB 类似,将对格式错误的地理空间数据抛出错误。 MongoDB Documentation on $polygon.另外要提的是,MongoDB 只接受 lng, lat 格式的坐标。
从Official Google Maps Documentation on Polygons 可以看到,您可以从坐标数组开始绘制多边形:
// Define the LatLng coordinates for the polygon.
var triangleCoords = [
{lat: 25.774, lng: -80.190},
{lat: 18.466, lng: -66.118},
{lat: 32.321, lng: -64.757}
];
然后你就可以构建它并在地图上设置它:
// Construct the polygon.
var bermudaTriangle = new google.maps.Polygon({
paths: triangleCoords, //Array of Coordinates
strokeColor: '#FF0000', //Color of the line
strokeOpacity: 0.8, //Color opacity
strokeWeight: 3, //Line weight
fillColor: '#FF0000', //Inner Color
fillOpacity: 0.35 //Inner color opacity
});
//Set it on the map
bermudaTriangle.setMap(map);
然后,您可以使用 clickListener 和 infoWindows 向多边形添加信息
// Add a listener for the click event and opens infoWindow
bermudaTriangle.addListener('click', showArrays);
infoWindow = new google.maps.InfoWindow;
您可以阅读有关 infoWindows here 的更多信息。
最后,here 你可以找到我使用 Google Maps Docs 和 AngularJS 制作的 Plunker。当然你可以用纯 Javascript 来做。
最后但并非最不重要的一点是,确保边界上有点并具有合理的lat, lng 坐标。
希望我对您有所帮助。
【讨论】: