Hug*_*o75 3 mapkit mkannotation mapkitannotation swift
我正在 MapKit/Swift 4 中开发地图,其中有很多注释。用户可以使用选择器视图选择他想要的注释(仅 1),并且该注释将被调用(就好像他已经按下了它一样)。如果方便的话,我们可以考虑改变这个注释点的颜色。关键是要在众多注释中突出显示这个特定的注释。
经过一番搜索,我发现了这个功能
func selectAnnotation(_ annotation: MKAnnotation, animated: Bool)
Run Code Online (Sandbox Code Playgroud)
我已经实现了以下功能:
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)
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我创建一个新注释,它就可以工作,但是我如何选择以前的注释点?但我没有任何继续前进的想法或暗示。
谢谢你的帮助。
编辑:输入注释的部分代码。
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
}
Run Code Online (Sandbox Code Playgroud)
您可以使用annotations您的财产来做到这一点mapView。作为一个粗略的轮廓,在你的视图控制器中你将有一些像这样的代码:
func beginAnnotationSelection() {
self.view.addSubview(self.pickerView)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.mapView.annotations.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return self.mapView.annotations[row].title ?? "No title"
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.mapView.selectAnnotation(self.mapView.annotations[row], animated: true)
}
Run Code Online (Sandbox Code Playgroud)
请注意,这假设mapView和pickerView是视图控制器的实例变量,并且选择器视图的数据源和委托设置为您的视图控制器。我没有为选择器视图或任何东西做任何框架设置,所以你必须自己实现所有这些。
| 归档时间: |
|
| 查看次数: |
4974 次 |
| 最近记录: |