是否可以将MGLShapes动态添加到MGLShapeSource

时间:2017-05-18 14:39:50

标签: ios mapbox

我想在单个MGLStyleLayer上以点云的形式跟踪用户的位置。这样就可以在以后显示和隐藏它。但我无法弄清楚如何修改MGLShapeSource。我唯一看到的方法是创建一个全新的MGLShape并将其分配给MGLShapeSource。但是我 只想在现有形状上添加一个点(每秒),点存储将快速增长......

图层创建如下:

func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle {
    addLayer(to: style)
}

private func addLayer(to style: MGLStyle) {

    // Create initial data
    let geoJson = [
        "type" : "FeatureCollection",
        "features" :
            [
                [
                    "type" : "Feature",
                    "geometry" : [
                        "type": "Point",
                        "coordinates": [0.0, 0.0]
                    ],
                    "properties" : [
                        "name" : "somewhere in the ocean"
                    ]
                ]
            ]
        ] as [String : Any]

    // Create layer with source and add them to style
    do {
        let geoJsonData = try JSONSerialization.data(withJSONObject: geoJson)
        let shape = try MGLShape(data: geoJsonData, encoding: String.Encoding.utf8.rawValue)
        let source = MGLShapeSource(identifier: "heatMapSource", shape: shape, options: nil)
        let layer = MGLCircleStyleLayer(identifier: "heatMapLayer", source: source)
        layer.sourceLayerIdentifier = "heatMap"
        layer.isVisible = true
        style.addSource(source)
        style.addLayer(layer)
        self.heatMapSource = source
        self.heatMapLayer = layer
    } catch {
        print("\(#function): \(error)")
    }
}

Mapbox版本是:Mapbox-iOS-SDK 3.5.4

编辑:重新分配新形状目前的工作方式如下:

// Create a 'modified' dictionary
let geoJson = [
    "type" : "FeatureCollection",
    "features" :
        [
            [
                "type" : "Feature",
                "geometry" : [
                    "type": "Point",
                    "coordinates": [0.0, 0.0]
                ],
                "properties" : [
                    "name" : "somewhere in the ocean"
                ]
            ],
            [
                "type" : "Feature",
                "geometry" : [
                    "type": "Point",
                    "coordinates": [1.0, 0.0]
                ],
                "properties" : [
                    "name" : "somewhere else in the ocean"
                ]
            ]
        ]
    ] as [String : Any]

// Assign new shape
do {
    let geoJsonData = try JSONSerialization.data(withJSONObject: geoJson)
    let shape = try MGLShape(data: geoJsonData, encoding: String.Encoding.utf8.rawValue)
    heatMapSource?.shape = shape
} catch {
    print("\(#function): \(error)")
}

0 个答案:

没有答案
相关问题