I want to track the location of a user in the form of a point cloud on a single MGLStyleLayer. That way it is possible to show and hide it later. But I can't figure out how to modify the MGLShapeSource. The only way I've seen is to create and assign a whole new MGLShape to the MGLShapeSource. But I only want to add a single point (every second) to an existing shape and the point storage will grow fast...
The layer is created as follows:
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 version is: Mapbox-iOS-SDK 3.5.4
EDIT: A re-assigning of a new shape is currently working like this:
// 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)")
}