我有大约50.000个要素的大型geojson数据。当我尝试将它们添加到mapView
发生“来自调试器的消息:由于内存问题而终止”。
我在30-35层之间划分了50.000个要素。内存使用高达1.3 GB。
首先,我尝试将geojson添加为MGLAnnotation
,它太慢并且仍然是相同的错误。而且我将geojson添加为MGLSymbolStyleLayer
,但仍然是相同的错误。
func drawPoint(geoJson : String , id: String) {
DispatchQueue.global(qos: .background).async(execute: {
do {
let data = geoJson.data(using: .utf8)
let kgmId = "kgm-\(id)"
guard let shapeCollectionFeature = try MGLShape(data: data!, encoding: String.Encoding.utf8.rawValue) as? MGLShapeCollectionFeature else {
fatalError("Could not cast to specified MGLShapeCollectionFeature")
}
self.removeLineLayer(type: kgmId)
let source = MGLShapeSource(identifier: kgmId, shape: shapeCollectionFeature, options: nil)
if self.mapView.style?.source(withIdentifier: kgmId) == nil{
self.mapView.style?.addSource(source)
}
let pointLayer = MGLSymbolStyleLayer(identifier: kgmId, source: source)
let zoomStops = [
13.49: NSExpression(forConstantValue: 0),
13.5: NSExpression(forConstantValue: 1)
]
pointLayer.iconOpacity = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", zoomStops)
let filteredLayer = layersArr.first(where: {$0.layer == id})
let type = filteredLayer?.style.type
if let type = type{
if type == "basic"{
pointLayer.iconImageName = NSExpression(forConstantValue: kgmId)
}else if type == "coded"{
let style = layersArr.first(where: {$0.layer == id})?.style
let fieldName = style?.data[0].field_name
if let fieldName = fieldName{
pointLayer.iconImageName = NSExpression(format: "FUNCTION(%@, 'valueForKeyPath:', %K)", self.poiIcons[id] as! [String:String],fieldName)
}
}
}
pointLayer.iconAllowsOverlap = NSExpression(forConstantValue: true)
if self.mapView.style?.layer(withIdentifier: id) == nil{
self.mapView.style!.addLayer(pointLayer)
}
} catch {
print("GeoJSON parsing failed")
}
})
}
将大型geojson数据添加到mapView的最佳方法是什么?