如何检测在MapView

时间:2016-08-29 12:31:17

标签: ios swift dictionary annotations

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

这是我的代码:

我的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, 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

    }

}

2 个答案:

答案 0 :(得分:8)

为此,您可以使用Annotation中选定的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
}    

答案 1 :(得分:4)

对于Swift 4

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    var selectedAnnotation = view.annotation
}