向具有多个自定义注释视图的地图视图添加集群注释
Adding cluster annotations to a map view, which has several custom annotation views
我有一个地图视图,它有两种类型的自定义注释视图。我想知道如何为这些视图添加不同类型的集群(取决于注释视图的类型)。目前,我已经尝试按照 WWDC 2017 的 session 237 "What's new in MapKit" 中的示例项目进行所有操作。但是当我注册我的集群视图时,它什么都不做(甚至没有被调用)。我想,那是因为我使用了自定义注释视图并且没有注册它们,而是使用 MKMapViewDelegate
方法 mapView(_:viewFor:)
。这是我注册自定义集群注释的代码(ClusterView
是 MKAnnotationView
的子类,我在其中定义了我的集群注释):
mapView.register(ClusterView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
上面的代码是在 viewDidLoad()
方法中定义的,但同样,它甚至没有被调用。所以,我认为我应该实施 MKMapViewDelegate
方法之一:mapView(_:clusterAnnotationForMemeberAnnotations:)
。问题是,我没有任何添加集群注释的经验,所以我不知道如何正确地实现它。几个星期以来,我一直在 Internet 上寻找一些示例,但还没有找到任何东西(仅关于第三方库)。如果您知道如何实现上述方法,或将集群添加到具有不同类型的自定义注释视图(不使用第三方库)的 mapView
,我将不胜感激。
尝试使用字符串重用标识符注册您的注释:
mapView.register(AnnotationViewMarker.self, forAnnotationViewWithReuseIdentifier: "marker")
mapView.register(AppleClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: "cluster")
我也遇到过这个问题。
如果这里有一个适用于我的应用程序的示例:
override func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation || annotation is UserAnnotation {
return nil
}
if let marker = annotation as? WifiAnnotation {
var view = mapView.dequeueReusableAnnotationView(withIdentifier: "marker") as? AnnotationViewMarker
if view == nil {
view = AnnotationViewMarker(annotation: marker, reuseIdentifier: "marker")
}
return view
} else if let cluster = annotation as? MKClusterAnnotation {
var view = mapView.dequeueReusableAnnotationView(withIdentifier: "cluster") as? AppleClusterAnnotationView
if view == nil {
view = AppleClusterAnnotationView(annotation: cluster, reuseIdentifier: "cluster")
}
return view
}
return nil
}
这里是我的 MKAnnotationView 的一个例子 Class:
class AppleClusterAnnotationView: MKAnnotationView {
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
displayPriority = .defaultLow
//collisionMode = .circle
centerOffset = CGPoint(x: 0, y: -10) // Offset center point to animate better with marker annotations
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var annotation: MKAnnotation? {
willSet {
if let cluster = newValue as? MKClusterAnnotation {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 40, height: 40))
let count = cluster.memberAnnotations.count
let uniCount = cluster.memberAnnotations.filter { member -> Bool in
return (member as! WifiAnnotation).wifiType != "Ad"
}.count
image = renderer.image { _ in
// Fill full circle with tricycle color
UIColor(red: 52/255, green: 131/255, blue: 223/255, alpha: 0.22).setFill()
UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 40, height: 40)).fill()
// Fill pie with unicycle color
UIColor(red: 52/255, green: 131/255, blue: 223/255, alpha: 0.22).setFill()
let piePath = UIBezierPath()
piePath.addArc(withCenter: CGPoint(x: 20, y: 20), radius: 20,
startAngle: 0, endAngle: (CGFloat.pi * 2.0 * CGFloat(uniCount)) / CGFloat(count),
clockwise: true)
piePath.addLine(to: CGPoint(x: 20, y: 20))
piePath.close()
piePath.fill()
// Fill inner circle with white color
UIColor.white.setFill()
UIBezierPath(ovalIn: CGRect(x: 8, y: 8, width: 24, height: 24)).fill()
// Finally draw count text vertically and horizontally centered
let attributes = [ NSAttributedStringKey.foregroundColor: UIColor.black,
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 15)]
let text = "\(count)"
let size = text.size(withAttributes: attributes)
let rect = CGRect(x: 20 - size.width / 2, y: 20 - size.height / 2, width: size.width, height: size.height)
text.draw(in: rect, withAttributes: attributes)
}
}
}
}}
最后是我的 MKMarkerAnnotationView。
有了它,您就可以进行所有设置以使其正常工作。您只需设置您的 MKAnnotation。
MKMarkerAnnotationView:
class AnnotationViewMarker: MKMarkerAnnotationView {
override open var annotation: MKAnnotation? {
willSet {
if let annotation = newValue as? WifiAnnotation {
clusteringIdentifier = "WifiAnnotation"
if annotation.wifiType == "yyy" {
glyphImage = UIImage(named: "yyy")
selectedGlyphImage = UIImage(named: "yyy")
markerTintColor = UIColor("#303030")
glyphTintColor = .white
displayPriority = .defaultHigh
titleVisibility = .visible
animatesWhenAdded = true
} else {
glyphImage = UIImage(named: "xxx")
selectedGlyphImage = UIImage(named: "xxx")
markerTintColor = UIColor("#3483DF")
glyphTintColor = .white
displayPriority = .required
titleVisibility = .visible
animatesWhenAdded = true
let accessButton = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
accessButton.setImage(self.accessAnnotation, for: .normal) //UIImage(named: "accessAnnotation"), for: .normal)
rightCalloutAccessoryView = accessButton
}
//collisionMode = .circle
}
}
}
}
我有一个地图视图,它有两种类型的自定义注释视图。我想知道如何为这些视图添加不同类型的集群(取决于注释视图的类型)。目前,我已经尝试按照 WWDC 2017 的 session 237 "What's new in MapKit" 中的示例项目进行所有操作。但是当我注册我的集群视图时,它什么都不做(甚至没有被调用)。我想,那是因为我使用了自定义注释视图并且没有注册它们,而是使用 MKMapViewDelegate
方法 mapView(_:viewFor:)
。这是我注册自定义集群注释的代码(ClusterView
是 MKAnnotationView
的子类,我在其中定义了我的集群注释):
mapView.register(ClusterView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
上面的代码是在 viewDidLoad()
方法中定义的,但同样,它甚至没有被调用。所以,我认为我应该实施 MKMapViewDelegate
方法之一:mapView(_:clusterAnnotationForMemeberAnnotations:)
。问题是,我没有任何添加集群注释的经验,所以我不知道如何正确地实现它。几个星期以来,我一直在 Internet 上寻找一些示例,但还没有找到任何东西(仅关于第三方库)。如果您知道如何实现上述方法,或将集群添加到具有不同类型的自定义注释视图(不使用第三方库)的 mapView
,我将不胜感激。
尝试使用字符串重用标识符注册您的注释:
mapView.register(AnnotationViewMarker.self, forAnnotationViewWithReuseIdentifier: "marker")
mapView.register(AppleClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: "cluster")
我也遇到过这个问题。 如果这里有一个适用于我的应用程序的示例:
override func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation || annotation is UserAnnotation {
return nil
}
if let marker = annotation as? WifiAnnotation {
var view = mapView.dequeueReusableAnnotationView(withIdentifier: "marker") as? AnnotationViewMarker
if view == nil {
view = AnnotationViewMarker(annotation: marker, reuseIdentifier: "marker")
}
return view
} else if let cluster = annotation as? MKClusterAnnotation {
var view = mapView.dequeueReusableAnnotationView(withIdentifier: "cluster") as? AppleClusterAnnotationView
if view == nil {
view = AppleClusterAnnotationView(annotation: cluster, reuseIdentifier: "cluster")
}
return view
}
return nil
}
这里是我的 MKAnnotationView 的一个例子 Class:
class AppleClusterAnnotationView: MKAnnotationView {
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
displayPriority = .defaultLow
//collisionMode = .circle
centerOffset = CGPoint(x: 0, y: -10) // Offset center point to animate better with marker annotations
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var annotation: MKAnnotation? {
willSet {
if let cluster = newValue as? MKClusterAnnotation {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 40, height: 40))
let count = cluster.memberAnnotations.count
let uniCount = cluster.memberAnnotations.filter { member -> Bool in
return (member as! WifiAnnotation).wifiType != "Ad"
}.count
image = renderer.image { _ in
// Fill full circle with tricycle color
UIColor(red: 52/255, green: 131/255, blue: 223/255, alpha: 0.22).setFill()
UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 40, height: 40)).fill()
// Fill pie with unicycle color
UIColor(red: 52/255, green: 131/255, blue: 223/255, alpha: 0.22).setFill()
let piePath = UIBezierPath()
piePath.addArc(withCenter: CGPoint(x: 20, y: 20), radius: 20,
startAngle: 0, endAngle: (CGFloat.pi * 2.0 * CGFloat(uniCount)) / CGFloat(count),
clockwise: true)
piePath.addLine(to: CGPoint(x: 20, y: 20))
piePath.close()
piePath.fill()
// Fill inner circle with white color
UIColor.white.setFill()
UIBezierPath(ovalIn: CGRect(x: 8, y: 8, width: 24, height: 24)).fill()
// Finally draw count text vertically and horizontally centered
let attributes = [ NSAttributedStringKey.foregroundColor: UIColor.black,
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 15)]
let text = "\(count)"
let size = text.size(withAttributes: attributes)
let rect = CGRect(x: 20 - size.width / 2, y: 20 - size.height / 2, width: size.width, height: size.height)
text.draw(in: rect, withAttributes: attributes)
}
}
}
}}
最后是我的 MKMarkerAnnotationView。 有了它,您就可以进行所有设置以使其正常工作。您只需设置您的 MKAnnotation。
MKMarkerAnnotationView:
class AnnotationViewMarker: MKMarkerAnnotationView {
override open var annotation: MKAnnotation? {
willSet {
if let annotation = newValue as? WifiAnnotation {
clusteringIdentifier = "WifiAnnotation"
if annotation.wifiType == "yyy" {
glyphImage = UIImage(named: "yyy")
selectedGlyphImage = UIImage(named: "yyy")
markerTintColor = UIColor("#303030")
glyphTintColor = .white
displayPriority = .defaultHigh
titleVisibility = .visible
animatesWhenAdded = true
} else {
glyphImage = UIImage(named: "xxx")
selectedGlyphImage = UIImage(named: "xxx")
markerTintColor = UIColor("#3483DF")
glyphTintColor = .white
displayPriority = .required
titleVisibility = .visible
animatesWhenAdded = true
let accessButton = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
accessButton.setImage(self.accessAnnotation, for: .normal) //UIImage(named: "accessAnnotation"), for: .normal)
rightCalloutAccessoryView = accessButton
}
//collisionMode = .circle
}
}
}
}