【发布时间】:2018-01-03 21:17:38
【问题描述】:
我正在使用 MapKit/Swift 4 开发地图,其中有很多注释。 用户可以使用 Picker View 选择他想要的注释(只有 1 个),并且注释将被调用(就好像他已经按下它一样)。 如果更简单,我们可以考虑改变这个注释点的颜色。 关键是要在众多注解中突出显示这个特定的注解。
经过一番搜索,我找到了这个功能
func selectAnnotation(_ annotation: MKAnnotation, animated: Bool)
我已经实现了以下功能:
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
}
【问题讨论】:
-
想必您已经在地图视图中添加了注释,对吗?只需调用上述函数,将您的注释之一作为参数传递。
-
确实如此。但这就是我的观点。我怎么能通过我的注释之一?我需要使用什么参考?其实,我正在寻找一个例子。
-
你会发布一些你的代码,比如你用来添加注释的代码,或者你的选择器代码吗?
标签: swift mapkit mkannotation mapkitannotation