我正在开发一个使用MapKit/Swift 4的地图,其中有很多注释。用户可以使用Picker View选择他想要的注释(仅限1个),并且该注释将被调用(就像他按下了它一样)。如果更容易的话,我们可以考虑改变这个注释点的颜色。关键是在众多注释中突出显示此特定注释。在一些搜索后,我找到了这个函数。
我已经实现了以下函数:
func selectAnnotation(_ annotation: MKAnnotation, animated: Bool)
我已经实现了以下函数:
function test():
func selectPoints() {
print("selectPoints called")
let annotation1 = MKPointAnnotation()
annotation1.coordinate = CLLocationCoordinate2D(latitude: 48.8596833, longitude: 2.3988939)
annotation1.title = "Temple"
annotation1.subtitle = "\(annotation1.coordinate.latitude), \(annotation1.coordinate.longitude)"
mapView.addAnnotation(annotation1)
mapView.selectAnnotation(annotation1, animated: true)
}
如果我创建一个新的注释,那么它就工作了,但是如何选择先前的注释点呢?但我没有任何想法或提示来继续。
谢谢你的帮助。
编辑:输入注释的代码部分。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? Artwork {
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.isDraggable = false
pinView!.calloutOffset = CGPoint(x: 0, y: 0)
pinView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView
}
else {
pinView!.annotation = annotation
}
}
return nil
}