1

我正在使用 Alamofire 从服务器请求数据,然后使用这些数据在地图上显示位置。平移或缩放地图时,需要请求 API 并重新加载地图。

当前的问题是,无论何时缩放或平移,CPU > 100% 都会导致无法缩放和平移。

我的代码如下:

阿拉莫火请求:

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)
        }
    })
}

添加/删除注释:

 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)
    }
}

地图委托:

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
    }
}

如何提高在 Mapkit 中重新加载注释的性能和最佳实践?

Xcode 控制台调试输出

4

1 回答 1

0

You are doing the API request on the main thread, a no-no.

You need to run the API request on a background thread, then update the map on the main thread. Like so:

DispatchQueue.global(qos: .userInitiated).async {
    SearchWebservice.sharedInstance.getResultMap(currentSearchObject, success: { (result) in

        //process the results

        //make sure to do any UI/Map stuff back on the main thread
        DispatchQueue.main.async(execute: { [unowned self] in
            self.mapView.addAnnotation(marker)
        })

    }
}
于 2017-02-03T05:08:44.017 回答