2

我有大约 50.000 个特征的大型 geojson 数据。当我尝试将它们添加到 mapView

出现“来自调试器的消息:由于内存问题而终止”。

我将 50.000 个特征划分为 30-35 层。内存使用高达 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 的最佳方法是什么?

4

0 回答 0