我的应用程序使用Geojson
文件。我使用MapBox SDK添加MGLPolyline
到地图。但问题是我的文件太大,以至于应用程序崩溃并得到错误:Message from debugger: Terminated due to memory issue
. 我在第一个循环中遇到了66234 个对象。我试图将数组分块为新数组,但没有成功。请帮我解决问题。这是我在地图上绘制的代码,这是我在 github 上的测试项目,使用 Xcode 8.1 如果有任何不同的 3rd 方可以解决我的问题,也欢迎:
func drawPolyline() {
// Parsing GeoJSON can be CPU intensive, do it on a background thread
DispatchQueue.global(qos: .background).async {
// Get the path for example.geojson in the app's bundle
let jsonPath = Bundle.main.path(forResource: "KMLMAPNew", ofType: "json")
let jsonData = NSData(contentsOfFile: jsonPath!)
do {
// Load and serialize the GeoJSON into a dictionary filled with properly-typed objects
guard let jsonDict = try JSONSerialization.jsonObject(with: jsonData! as Data, options: []) as? Dictionary<String, AnyObject>, let features = jsonDict["features"] as? Array<AnyObject> else{return}
for feature in features {
guard let feature = feature as? Dictionary<String, AnyObject>, let geometry = feature["geometry"] as? Dictionary<String, AnyObject> else{ continue }
if geometry["type"] as? String == "LineString" {
// Create an array to hold the formatted coordinates for our line
var coordinates: [CLLocationCoordinate2D] = []
if let locations = geometry["coordinates"] as? Array<AnyObject> {
// Iterate over line coordinates, stored in GeoJSON as many lng, lat arrays
for location in locations {
// Make a CLLocationCoordinate2D with the lat, lng
if let location = location as? Array<AnyObject>{
let coordinate = CLLocationCoordinate2DMake(location[1].doubleValue, location[0].doubleValue)
// Add coordinate to coordinates array
coordinates.append(coordinate)
}
}
}
let line = MGLPolyline(coordinates: &coordinates, count: UInt(coordinates.count))
// Optionally set the title of the polyline, which can be used for:
// - Callout view
// - Object identification
line.title = "Crema to Council Crest"
// Add the annotation on the main thread
DispatchQueue.main.async {
// Unowned reference to self to prevent retain cycle
[unowned self] in
self.mapboxView.addAnnotation(line)
}
}
}
}
catch
{
print("GeoJSON parsing failed")
}
}
}
编辑:: @Alessandro Ornano 和@fragilecat 非常感谢。但这些解决方案仍然无法解决 iPad 上应用程序的终止问题。我认为很难更改当前代码以使其正常工作,因为数据如此之大。我想我需要另一种适用于大数据的解决方案。就像将数组分块成小数组然后按队列加载它们一样。但我不知道如何开始:(
我向 MapBox 的支持团队发送了一封电子邮件,征求建议。