我正在使用谷歌地图来定义城市的区域。用户绘制多边形以确定所需区域。为了达到这个目标,我指的是DrawingManager文章this article。一切顺利,我想更新注册区域并绘制多边形。我要遵循的路径是使用DrawingManager。这是我的代码。该代码可正确加载地图,但不会根据给定坐标绘制多边形。

GoogleMap.prototype.initial = function (currentPolygonData) {
var self = this;
var triangleCoords = [];
if (currentPolygonData != null && currentPolygonData.length>0) {
    triangleCoords = [];
    for (var i = 0; i < currentPolygonData.length; i++) {
        triangleCoords.push(new google.maps.LatLng(currentPolygonData[i].Lat, currentPolygonData[i].Lng));
    }
}
var polyOptions = {
    paths: triangleCoords,
    strokeColor: '#bb4446',
    fillColor: '#c37172',
    strokeWeight: 0,
    fillOpacity: 0.45,
    editable: true,
    draggable: true,
};

self.drawingManager = new google.maps.drawing.DrawingManager({
    drawingMode: google.maps.drawing.OverlayType.POLYGON,
    drawingControlOptions: {
        drawingModes: [
            google.maps.drawing.OverlayType.POLYGON
        ]
    },
    markerOptions: {
        draggable: true

    },
    polylineOptions: {
        editable: true,
        draggable: true

    },
    rectangleOptions: polyOptions,
    circleOptions: polyOptions,
    polygonOptions: polyOptions,
    map: self.map
});}

最佳答案

LatLng集合的数据类型应为字典数组,键的纬度和经度分别为'lat'和'lng'。

因此,在您的代码中应该是

var triangleCoordsArr = {};
if (currentPolygonData != null && currentPolygonData.length>0) {
    for (var i = 0; i < currentPolygonData.length; i++) {
        triangleCoords = {};
        triangleCoords['lat'] = currentPolygonData[i].Lat;
        triangleCoords['lng'] = currentPolygonData[i].Lng;
        triangleCoordsArr.push(triangleCoords)
    }
}
for (var i = 0; i < triangleCoordsArr.length; i++) {
    // Construct the polygon.
    var mTriangle = new google.maps.Polygon({
        paths: triangleCoordsArr[i],
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35
    });
    mTriangle.setMap(map);
}


另外,此reference link from Maps Documentation在此应有助于获取更多信息来解决您的问题

10-06 03:30