ios – 如何检测在MapView中选择了哪个注释

前端之家收集整理的这篇文章主要介绍了ios – 如何检测在MapView中选择了哪个注释前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在地图中做了一些注释,当我点击它们时,我看到一些信息,我有一个按钮打开地图,并且有正确的信息我不能接受它应该画我的路线.

这是我的代码

我的lat和amp有2个双列阵列.我从我的询问中填写了他们.

  1. var lat = [Double]()
  2. var lon = [Double]()

这些行用于填充注释

  1. self.annot = Islands(title: object["name"] as! String,subtitle: object["address"] as! String,coordinate: CLLocationCoordinate2D(latitude: (position?.latitude)!,longitude: (position?.longitude)!),info: object["address"] as! String)
  2.  
  3. self.map.addAnnotations([self.annot])

这是注释创建的位置:

  1. func mapView(mapView: MKMapView,viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
  2.  
  3. let identifier = "pin"
  4.  
  5. if annotation.isKindOfClass(MKUserLocation) {
  6. return nil
  7. }
  8.  
  9. let image = UIImage(named: "car")
  10. let button = UIButton(type: .Custom)
  11. button.frame = CGRectMake(0,30,30)
  12. button.setImage(image,forState: .Normal)
  13. button.addTarget(self,action: #selector(Map.info(_:)),forControlEvents:UIControlEvents.TouchUpInside)
  14.  
  15. var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
  16. if annotationView == nil {
  17. annotationView = MKAnnotationView(annotation: annotation,reuseIdentifier: "pin")
  18. annotationView!.canShowCallout = true
  19. annotationView!.image = UIImage(named: "annotation")
  20. annotationView!.rightCalloutAccessoryView = button
  21.  
  22. }
  23. else {
  24. annotationView!.annotation = annotation
  25. }
  26.  
  27. return annotationView
  28.  
  29. }

最后这是按钮的功能,我应该传递正确的信息(以及问题出在哪里)

  1. func info(sender: UIButton)
  2. {
  3.  
  4. let latitute:CLLocationDegrees = lat[sender.tag]
  5. let longitute:CLLocationDegrees = lon[sender.tag]
  6.  
  7. let regionDistance:CLLocationDistance = 10000
  8. let coordinates = CLLocationCoordinate2DMake(latitute,longitute)
  9. let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates,regionDistance,regionDistance)
  10. let options = [
  11. MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
  12. ]
  13. let placemark = MKPlacemark(coordinate: coordinates,addressDictionary: nil)
  14. let mapItem = MKMapItem(placemark: placemark)
  15. mapItem.name = name[sender.tag]
  16. print(sender.tag)
  17. mapItem.openInMapsWithLaunchOptions(options)
  18. }

如你所见,我尝试使用发送者标签,但没有运气.

编辑:我的自定义注释类

  1. class Islands: NSObject,MKAnnotation {
  2.  
  3. var title: String?
  4. var addr: String?
  5. var coordinate: CLLocationCoordinate2D
  6. var info: String?
  7. var subtitle: String?
  8.  
  9. init(title: String,subtitle: String,coordinate: CLLocationCoordinate2D,info: String) {
  10. self.title = title
  11. self.coordinate = coordinate
  12. self.info = info
  13. self.subtitle = subtitle
  14.  
  15. }
  16.  
  17. }

解决方法

为此,您可以使用didSelectAnnotationView中的选定注释,然后将该注释存储到实例变量,然后在Button操作方法中使用注释.
  1. var selectedAnnotation: MKPointAnnotation?
  2.  
  3. func mapView(mapView: MKMapView,didSelectAnnotationView view: MKAnnotationView) {
  4. self.selectedAnnotation = view.annotation as? MKPointAnnotation
  5. }
  6.  
  7. func info(sender: UIButton) {
  8. print(selectedAnnotation?.coordinate)
  9. }

编辑:你有自定义MKAnnotation类,你需要使用它.

  1. var selectedAnnotation: Islands?
  2.  
  3. func mapView(mapView: MKMapView,didSelectAnnotationView view: MKAnnotationView) {
  4. self.selectedAnnotation = view.annotation as? Islands
  5. }

猜你在找的iOS相关文章