I am developing Mapview
in swift language. For, that I am loading geojson file into my mapview, its loading successfully. I am loading with GEOSwift
library. Its working fine, but, what happening, While loading data, the mapview getting freeze/stuck for few seconds in UI, then later showing data. Even while Zooming time also freezing the UI few seconds.
I am loading LineString
type data with coordinates around 7000 data.
Even I am doing parsing the data in background thread only.
I have two 3 tabs for loading different data in my map with 3 geojson
files. Even while switching one data to another data, its getting freezing.
In debugging are following error printing while zoomin/zoomout the mapview.
[VKDefault] Tile 11720.7599.14 (128) in current unloaded state for 0.01 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.01 sec)
My code is follows.
@IBAction func mapDataAction(_ sender: Any) {
let overlays = mapView.overlays
mapView.removeOverlays(overlays)
self.mapView.removeAnnotations(mapView.annotations)
DispatchQueue.global(qos: .background).async {
if let geoJSONURL = Bundle.main.url(forResource: “LineString”, withExtension: "geojson") {
do {
var overlays = [MKPolyline]()
let features = try Features.fromGeoJSON(geoJSONURL)
for item in features! {
if let item = item.geometries![0] as? LineString {
let polyLine = item.mapShape() as! MKPolyline
overlays.append(polyLine)
}
}
DispatchQueue.main.async {
// add overlays to map
self.mapView.addOverlays(overlays)
}
} catch {
//handle error
}
}
}
}
And one time app gets crashed with following message thrown in console.
Terminated due to memory issue
And its taking huge memory from cpu of device.
While scrolling the mapview or zoomin/zoomout its calling always the delegate method, this making freezing the mapview.
public func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let polyline = overlay as? MKPolyline {
let renderer = MKPolylineRenderer(polyline: polyline)
//renderer.fillColor = UIColor.blue.withAlphaComponent(0.5)
renderer.strokeColor = UIColor.blue.withAlphaComponent(0.8)
renderer.lineWidth = 5
print("MKPolylineRenderer delegate calling")
return renderer
}
return MKOverlayRenderer(overlay: overlay)
// fatalError("Unexpected overlay type")
}
How can I solve this?