如何以编程方式使用谷歌地图DrawManager绘制多边形

时间:2017-09-05 10:52:56

标签: javascript google-maps drawing

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

fileprivate let coordinator: NSPersistentStoreCoordinator
let mainContext: NSManagedObjectContext
var isLoaded = false

init() {
    guard let model = NSManagedObjectModel.mergedModel(from: nil) else {
        fatalError("No models in main bundle")
    }

    coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
    coordinator.name = "main_coordinator"

    mainContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    mainContext.name = "main_context"
    mainContext.persistentStoreCoordinator = coordinator
    mainContext.undoManager = nil

    let center = NotificationCenter.default
    center.addObserver(self, selector: #selector(save), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
    center.addObserver(self, selector: #selector(save), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
}

1 个答案:

答案 0 :(得分:1)

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应该会为您提供更多信息来解决您的情况