我有一些自定义注释,有时会在它们上方显示一个textView。 如果在我的注释上名为text的变量为nil,则它们不会显示textView。
注释可能要显示文本,但是在显示注释时text变量的值可能会更改。在这种情况下,我希望刷新注记,以使其不再显示textView。
我已经有了一个委托函数,如果设置了注释文本变量,则可以使用textView创建一个注释,如果未设置注释的文本变量,则可以创建不具有textView的注释,尽管如此,不是实际的代码
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView?{
if annotation is MyCustomAnnotation{
if annotation.hasText(){
return MyCustomAnnotationView(hasText: True)
}else{
return ViewWithoutTextView(hasText: False)
}
}
但是,如果在已显示注释的情况下注释从有文本变为无文本,反之亦然,那么我不知道如何刷新或再次调用它以显示正确的注释视图>
答案 0 :(得分:1)
正如@Magnas 在评论中所说,您必须删除注释并重新添加它以更新状态。
最好创建一个自定义注释视图,该视图具有处理隐藏/显示其中的文本视图的逻辑。然后,您只需保留注释的引用并通过 annotationView
更新该引用,而根本不会经历和弄乱地图注释。
一个粗略的例子(很多空白要填):
// your methods in your custom annotation. Use these wherever you want to change things
class CustomAnnotation: MGLAnnotationView {
func showText() { }
func hideText() { }
}
// Define data structure to access your annotation with some kind of key
dataSourceToAnnotationView: [String: CustomAnnotation]
// save your annotations so you can access them later
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "customReuseId")
if annotationView == nil {
annotationView = CustomAnnotation()
let key = "exampleKeyString"
dataSourceToAnnotationView[key] = annotationView as! CustomAnnotation
}
return annotationView
}