I'm using Alamofire to request data from server, then use these data to show location on map. When panning or zooming map, it required to request API and reload the map.
The current issue is that whenever zooming or panning, CPU > 100% lead to the inability to zoom and pan.
My code is below:
Alamofire Request:
func getResultMap() {
DispatchQueue.main.async( execute: {
self.skipSearchRequest = true
SearchWebservice.sharedInstance.getResultMap(currentSearchObject, success: { (result) in
var clusters: [Cluster] = []
var markers : [Marker] = []
if self.mapLayerType == .ClusterOverview {
for item in result.clusterOverview {
clusters.append(item)
}
self.setMapAnnotations(annotations: clusters)
} else {
for item in result.markers {
markers.append(item)
}
self.addMapAnnotations(annotations: markers)
}
self.skipSearchRequest = false
}) { (error) in
print(error)
}
})
}
Add/Remove Annotation:
func addMapAnnotations(annotations: [MKAnnotation]) {
var annotationsToRemove :[MKAnnotation] = []
var annotationsToAdd: [MKAnnotation] = annotations
let annotationsTemp: [MKAnnotation] = annotations
for item in mapView.annotations {
if item.isKind(of: Cluster.self) {
annotationsToRemove.append(item)
} else if item.isKind(of: Marker.self) {
for itemTemp in annotationsTemp {
if itemTemp.coordinate.latitude == item.coordinate.longitude && itemTemp.coordinate.longitude == item.coordinate.longitude {
annotationsToAdd.remove(at: annotationsToAdd.index(where: {$0.coordinate.latitude == itemTemp.coordinate.latitude && $0.coordinate.longitude == itemTemp.coordinate.longitude})!)
}
}
}
}
mapView.removeAnnotations(annotationsToRemove)
mapView.addAnnotations(annotationsToAdd)
}
func setMapAnnotations(annotations: [MKAnnotation]) {
self.mapView.removeAnnotations(self.mapView.annotations)
var annotationsToAdd: [MKAnnotation] = []
for item in annotations {
if item.isKind(of: Cluster.self) {
annotationsToAdd.append(item)
} else if item.isKind(of: Marker.self) {
annotationsToAdd.append(item)
}
}
DispatchQueue.main.async {
self.mapView.addAnnotations(annotationsToAdd)
}
}
Map delegate:
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if !skipSearchRequest {
Timer.scheduledTimer(timeInterval: 0.7, target: self, selector: #selector(COHFResultMapViewController.getResultMap), userInfo: nil, repeats: false)
} else {
skipSearchRequest = false
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: Cluster.self) {
let reuseId = "Cluster"
var clusterView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? ClusterView
if clusterView == nil {
clusterView = ClusterView(annotation: annotation, reuseIdentifier: reuseId)
} else {
clusterView?.annotation = annotation
}
return clusterView
} else {
let reuseId = "Pin"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if annotationView == nil {
annotationView = AnnotationView(annotation: annotation, reuseIdentifier: reuseId)
} else {
annotationView?.annotation = annotation
}
return annotationView
}
}
How to improve performance and best practice for reloading annotation in Mapkit?