首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapView 11中iOS集群性能差

MKMapView 11中iOS集群性能差
EN

Stack Overflow用户
提问于 2018-03-13 21:24:07
回答 1查看 2.3K关注 0票数 15

我一直使用第三方库来处理标记聚类。由于iOS 11有自己的实现,所以我决定代表“本机”实现继续并删除第三方库。

我从WWDC 2017下载了示例应用程序,并遵循了相同的步骤,因此:

  • 连接MKMapView插座
  • 实现my模型的MKAnnotation协议
  • 为标记视图创建MKMarkerAnnotationView
  • 为集群视图创建一个MKMarkerAnnotationView
  • 用寄存器(_:forAnnotationViewWithReuseIdentifier:)函数在我的_:forAnnotationViewWithReuseIdentifier:引用上注册两个注释
  • 向我的地图添加注释

然而,当使用第三方库时,一切都很好,使用这种方法,当我在mapView和更改区域上运行时,性能非常差。CPU使用率提高了90%,而内存似乎保持稳定,我感到移动延迟,有时甚至是应用程序崩溃。我正在加载大约600个注释。

有什么建议吗?

以下是代码:

代码语言:javascript
复制
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
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-03-17 17:30:18

这里,您可以读到:

不要自己创建该类的实例。当两个或多个注释视图在映射面上过于紧密地组合在一起时,MapKit会自动创建集群注释。

您可以尝试不自己创建该类的实例。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49266084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档