我一直使用第三方库来处理标记聚类。由于iOS 11有自己的实现,所以我决定代表“本机”实现继续并删除第三方库。
我从WWDC 2017下载了示例应用程序,并遵循了相同的步骤,因此:
MKMapView插座MKAnnotation协议MKMarkerAnnotationViewMKMarkerAnnotationView_:forAnnotationViewWithReuseIdentifier:)函数在我的_:forAnnotationViewWithReuseIdentifier:引用上注册两个注释然而,当使用第三方库时,一切都很好,使用这种方法,当我在mapView和更改区域上运行时,性能非常差。CPU使用率提高了90%,而内存似乎保持稳定,我感到移动延迟,有时甚至是应用程序崩溃。我正在加载大约600个注释。
有什么建议吗?
以下是代码:
class MapViewClusteringViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
private var databaseService: LocalDataService!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.register(StopMarker.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
mapView.register(StopCluster.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
databaseService.fetchMarkers { markers in
mapView.addAnnotation(markers)
}
}
}
class StopMarker: MKMarkerAnnotationView {
override var annotation: MKAnnotation? {
willSet {
clusteringIdentifier = "busStopCluster"
subtitleVisibility = .adaptive
markerTintColor = .red
}
}
}
class StopCluster: MKMarkerAnnotationView {
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
displayPriority = .defaultHigh
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var annotation: MKAnnotation? {
willSet {
if let cluster = newValue as? MKClusterAnnotation {
markerTintColor = .green
glyphText = "\(cluster.memberAnnotations.count)"
}
}
}
}
class StopAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
init(coordinate: CLLocationCoordinate2D, title: String) {
self.coordinate = coordinate
self.title = title
}
}发布于 2018-03-17 17:30:18
https://stackoverflow.com/questions/49266084
复制相似问题