如何检测在MapView中选择了哪个注释 [英] How can I detect which annotation was selected in MapView

查看:200
本文介绍了如何检测在MapView中选择了哪个注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在地图中做了一些注释,当我点击它们时,我看到了一些信息,我有一个按钮打开地图,并且有正确的信息我不能接受它应该画我的路线。 / p>

这是我的代码:



我的lat和amp有2个双数组lon我从查询中填写了它们。

  var lat = [Double]()
var lon = [Double ]()

这些行用于填写注释

  self.annot = Islands(title:object [name] as!String,subtitle:object [address] as!String,coordinate:CLLocationCoordinate2D(纬度:(位置) ?.latitude)!,经度:(位置?.longitude)!),info:object [address] as!String)

self.map.addAnnotations([self.annot])

这是注释创建的地方:

  func mapView(mapView:MKMapView,viewForAnnotation annotation:MKAnnotation) - > MKAnnotationView? {

let identifier =pin

if annotation.isKindOfClass(MKUserLocation){
return nil
}

let image = UIImage(命名为:car)
let button = UIButton(type:.Custom)
button.frame = CGRectMake(0,0,30,30)
button.setImage (image,forState:.Normal)
button.addTarget(self,action:#selector(Map.info(_ :)),forControlEvents:UIControlEvents.TouchUpInside)

var annotationView = mapView .dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation:annotation,reuseIdentifier:pin)
annotationView!.canShowCallout = true
annotationView!。 image = UIImage(命名:annotation)
annotationView!.rightCalloutAccessoryView = button

}
else {
annotationView!.annotation = annotation
}

返回annotationView

}

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

  func info(发件人:UIButton)
{

let latitute:CLLocationDegrees = lat [sender.tag]
let longitute:CLLocationDegrees = lon [sender.tag]

let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitute,longitute)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates,regionDistance,regionDistance)
let options = [
MKLaunchOptionsMapCenterKey:NSValue(MKCoordinate:regionSpan.center),
MKLaunchOptionsMapSpanKey:NSValue(MKCoordinateSpan:regionSpan.span )
]
let placemark = MKPlacemark(坐标:coordinates,addressDictionary:nil)
let mapItem = MKMapItem(地标:地标)
mapItem.name = na me [sender.tag]
print(sender.tag)
mapItem.openInMapsWithLaunchOptions(options)
}

如你所见,我尝试使用发件人标签,但没有运气。



编辑:我的自定义注释类

  class Islands:NSObject,MKAnnotation {

var title:String?
var addr:String?
var坐标:CLLocationCoordinate2D
var info:String?
var subtitle:String?

init(title:String,subtitle:String,coordinate:CLLocationCoordinate2D,info:String){
self.title = title
self.coordinate = coordinate
self .info = info
self.subtitle =副标题

}

}


解决方案

为此,您可以使用 didSelectAnnotationView中选定的注释 ,然后将该注释存储到实例变量中,然后在 Button 操作方法中使用注释。

  var selectedAnnotation:MKPointAnnotation? 

func mapView(mapView:MKMapView,didSelectAnnotationView视图:MKAnnotationView){
self.selectedAnnotation = view.annotation as? MKPointAnnotation
}

func info(发件人:UIButton){
print(selectedAnnotation?.coordinate)
}

编辑:您需要使用自定义 MKAnnotation 类那个。

  var selectedAnnotation:Islands? 

func mapView(mapView:MKMapView,didSelectAnnotationView视图:MKAnnotationView){
self.selectedAnnotation = view.annotation as?群岛
}


I've made a few annotations inside the Map and when I tap on them I see some informations and I have a button that opens Maps and with the right information that I can't take it should draw me my route.

Here is my code:

I have 2 arrays of double for my lat & lon That I filled them from my query.

 var lat = [Double]()
 var lon = [Double]()

These lines are for filling annotations

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)

self.map.addAnnotations([self.annot])

This is where the annotation is creating:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    let identifier = "pin"

    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    let image = UIImage(named: "car")
    let button = UIButton(type: .Custom)
    button.frame = CGRectMake(0, 0, 30, 30)
    button.setImage(image, forState: .Normal)
    button.addTarget(self, action: #selector(Map.info(_:)), forControlEvents:UIControlEvents.TouchUpInside)

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
    if  annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView!.canShowCallout = true
        annotationView!.image = UIImage(named: "annotation")
        annotationView!.rightCalloutAccessoryView = button

    }
    else {
        annotationView!.annotation = annotation
    }

    return annotationView

}

And finally this is the button's function where I should pass the right info ( and where the problem is)

   func info(sender: UIButton)
    {

        let latitute:CLLocationDegrees  =  lat[sender.tag]
        let longitute:CLLocationDegrees =  lon[sender.tag]

        let regionDistance:CLLocationDistance = 10000
        let coordinates = CLLocationCoordinate2DMake(latitute, longitute)
        let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
        let options = [
            MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
            MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
        ]
        let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = name[sender.tag]
        print(sender.tag)
        mapItem.openInMapsWithLaunchOptions(options)
    }

As you can see I tried with sender tag but with no luck.

EDIT: My Custom Annotation class

class Islands: NSObject, MKAnnotation {

    var title: String?
    var addr: String?
    var coordinate: CLLocationCoordinate2D
    var info: String?
    var subtitle: String?

    init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D, info: String) {
        self.title = title 
        self.coordinate = coordinate
        self.info = info
        self.subtitle = subtitle

    }

}

解决方案

For that you can use selected Annotation from didSelectAnnotationView, then store that annotation to instance variable and after that used annotation in your Button action method.

var selectedAnnotation: MKPointAnnotation?

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    self.selectedAnnotation = view.annotation as? MKPointAnnotation
}

func info(sender: UIButton) {
    print(selectedAnnotation?.coordinate)
}

Edit: As of you have custom MKAnnotation class you need to use that.

var selectedAnnotation: Islands?

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    self.selectedAnnotation = view.annotation as? Islands
}    

这篇关于如何检测在MapView中选择了哪个注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆