【问题标题】:How to get an array of points that would build a Polygon google map如何获得将构建多边形谷歌地图的点数组
【发布时间】:2016-11-12 11:58:20
【问题描述】:

我想用google MAP建立一个多边形区域作为图片,你知道如何获取该区域的坐标数组吗?

picture of desired polygon

【问题讨论】:

标签: google-maps dictionary polygon


【解决方案1】:

您可以使用包含至少 3 个元素的 lat, lng 对的数组来构建多边形。

如果数组没有关闭,谷歌地图会默认关闭它。

虽然对于大多数 DBMS,您必须保存一个封闭的坐标数组。关闭是什么意思?本质上,数组的第一个点和最后一个点是相等的。 DBMSMongoDB 类似,将对格式错误的地理空间数据抛出错误。 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);

然后,您可以使用 clickListenerinfoWindows 向多边形添加信息

// 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 坐标。

希望我对您有所帮助。

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 2015-02-05
    • 2012-07-01
    • 1970-01-01
    • 2012-08-28
    相关资源
    最近更新 更多