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

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

概述我在地图中做了一些注释,当我点击它们时,我看到一些信息,我有一个按钮打开地图,并且有正确的信息我不能接受它应该画我的路线. 这是我的代码: 我的lat和amp有2个双列阵列.我从我的询问中填写了他们. var lat = [Double]() var lon = [Double]() 这些行用于填充注释 self.annot = Islands(title: object["name"] as! 我在地图中做了一些注释,当我点击它们时,我看到一些信息,我有一个按钮打开地图,并且有正确的信息我不能接受它应该画我的路线.

这是我的代码:

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

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

这些行用于填充注释

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])

这是注释创建的位置:

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,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}

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

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)    }

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

编辑:我的自定义注释类

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    }}
解决方法 为此,您可以使用dIDSelectAnnotationVIEw中的选定注释,然后将该注释存储到实例变量,然后在button *** 作方法中使用注释.
var selectedAnnotation: MKPointAnnotation?func mapVIEw(mapVIEw: MKMapVIEw,dIDSelectAnnotationVIEw vIEw: MKAnnotationVIEw) {    self.selectedAnnotation = vIEw.annotation as? MKPointAnnotation}func info(sender: UIbutton) {    print(selectedAnnotation?.coordinate)}

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

var selectedAnnotation: Islands?func mapVIEw(mapVIEw: MKMapVIEw,dIDSelectAnnotationVIEw vIEw: MKAnnotationVIEw) {    self.selectedAnnotation = vIEw.annotation as? Islands}
总结

以上是内存溢出为你收集整理的ios – 如何检测在MapView中选择了哪个注释全部内容,希望文章能够帮你解决ios – 如何检测在MapView中选择了哪个注释所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1104154.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存