[英]Set annotation's glyphtext based on the annotation's properties
I'm running into an issue with custom annotations displaying incorrectly.我遇到了自定义注释显示不正确的问题。 In my code, I check whether the current annotation is for a station with a given unique identifier.
在我的代码中,我检查当前注释是否针对具有给定唯一标识符的电台。 If so, I customize its properties.
如果是这样,我会自定义其属性。
StationAnnotationView.swift StationAnnotationView.swift
class StationAnnotationView: MKMarkerAnnotationView {
override var annotation: MKAnnotation? {
willSet {
guard let station = newValue as? Station else { return }
clusteringIdentifier = nil
displayPriority = .required
if (station.id == "26") {
glyphText = "p"
markerTintColor = UIColor(named: "Blue")
}
}
}
At first, my mapView
displays the annotations correctly (ie, changing the color and glyphtext for the only station with station.id == 26
), but after panning and zooming for a while, my custom formatting begins to get applied to other annotations (which shouldn't happen, because there's only one station for any given station.id
).起初,我的
mapView
正确显示注释(即,更改唯一具有station.id == 26
站的颜色和字形文本),但是在平移和缩放一段时间后,我的自定义格式开始应用于其他注释(这不应该发生,因为任何给定的station.id
只有一个站)。 I suspect it's due to the AnnotationView
reusing the annotation.我怀疑这是由于
AnnotationView
重用了注释。 How can I prevent this from happening?我怎样才能防止这种情况发生?
As you said, it's due to the AnnotationView reusing the annotation.正如您所说,这是由于 AnnotationView 重用了注释。 Try the following code:
试试下面的代码:
if (station.id == "26") {
glyphText = "p"
markerTintColor = UIColor(named: "Blue")
} else {
glyphText = // Default text
markerTintColor = // Default color
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.