swift - 为什么我的 MKAnnotationView 没有显示?- 斯威夫特
问题描述
我的注释工作,但只要我添加此代码,注释就会停止出现。我究竟做错了什么?
mapview.delegate = self
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")
if annotationView == nil { annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView") }
if annotation.subtitle == "Innovation" { annotationView?.backgroundColor = UIColor(named: "Innovation")
} else if annotation.subtitle == "Art" { annotationView?.backgroundColor = UIColor(named: "Art")
} else if annotation.subtitle == "Entertainment" { annotationView?.backgroundColor = UIColor(named: "Entertainment")
} else if annotation.subtitle == "Networking" { annotationView?.backgroundColor = UIColor(named: "Networking")
} else if annotation.subtitle == "Sale" { annotationView?.backgroundColor = UIColor(named: "Sale")
} else if annotation.subtitle == "Sports" { annotationView?.backgroundColor = UIColor(named: "Sports")
} else if annotation.subtitle == "Food & Drink" { annotationView?.backgroundColor = UIColor(named: "Food & Drink")
} else if annotation.subtitle == "Community" { annotationView?.backgroundColor = UIColor(named: "Community")
} else if annotation.subtitle == "Spiritual" { annotationView?.backgroundColor = UIColor(named: "Spiritual") }
else { annotationView?.annotation = annotation}
return annotationView
}
let myCoordinate = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
let mapAnnotation = MKPointAnnotation()
mapAnnotation.title = title
mapAnnotation.subtitle = color
mapAnnotation.coordinate = myCoordinate
self.mapView.addAnnotation(mapAnnotation)
** 我没有自定义注释视图,因为我想做的只是改变气球别针的颜色 **
解决方案
您需要设置 MapView 的委托,并将注解注册到具有相同标识符的地图视图。
尝试
Map.register(MKAnnotationView.self, forAnnotationViewWithReuseIdentifier: "SomeRandomIdentifier")
希望能解决问题。
推荐阅读
- reactjs - 使用 reactjs 禁用或激活按钮
- flutter - Flutter中如何在两个页面上实现动画textField
- ios - Flutter -ios - pod install gRPC-C++ sed ssl 问题
- javascript - 在 Java Script 上找到我自己的公共 IP 地址?
- python - 为什么openCV“阻止”我的python脚本?
- python - 在显示面上管理多个 Tkinter Windows 的大小
- php - 如何在 Eloquent 中加入关系?
- javascript - HTML 按钮不适用于内联 javascript
- apache - macos mamp 上的虚拟主机 ERR_CONNECTION_REFUSED
- tensorflow - 当我更改输出层的激活函数时,模型不会学习