Ios 删除MKMapView Swift中的Pin注释问题

Ios 删除MKMapView Swift中的Pin注释问题,ios,swift,mkmapview,uigesturerecognizer,uitapgesturerecognizer,Ios,Swift,Mkmapview,Uigesturerecognizer,Uitapgesturerecognizer,我有一个地图视图,它允许我使用ui长按手势识别器向地图添加注释及其信息。然后,我添加了一个uitappesturerecognizer,希望在用户单击地图时删除该注释。它工作得很好 我遇到的问题是,当我点击pin时,它也会将其删除。我想能够点击pin显示它的信息,然后能够点击地图视图并删除pin class ViewController: UIViewController { @IBOutlet weak var myMap: MKMapView! let annotation = MKPo

我有一个地图视图,它允许我使用
ui长按手势识别器
向地图添加注释及其信息。然后,我添加了一个
uitappesturerecognizer
,希望在用户单击地图时删除该注释。它工作得很好

我遇到的问题是,当我点击pin时,它也会将其删除。我想能够点击pin显示它的信息,然后能够点击地图视图并删除pin

class ViewController: UIViewController {

@IBOutlet weak var myMap: MKMapView!

let annotation = MKPointAnnotation()

override func viewDidLoad() {
    super.viewDidLoad()

    let location = CLLocationCoordinate2D(latitude: 32.92, longitude: -96.46)
    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegion(center: location, span: span)

    myMap.setRegion(region, animated: true)


    var longPress = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
    longPress.minimumPressDuration = 2.0
    myMap.addGestureRecognizer(longPress)

    var tap = UITapGestureRecognizer(target: self, action: "removeAnnotation:")
    tap.numberOfTapsRequired = 1
    self.myMap.addGestureRecognizer(tap)


}
删除注释功能:

func removeAnnotation(gesture: UIGestureRecognizer) {

    if gesture.state == UIGestureRecognizerState.Ended {

        self.myMap.removeAnnotation(annotation)
        println("Annotation Removed")

    }

}
func addAnnotation(gesture: UIGestureRecognizer) {

    if gesture.state == UIGestureRecognizerState.Began {

        println("Long Press Started")

        var touch = gesture.locationInView(self.myMap)
        var coordinate = myMap.convertPoint(touch, toCoordinateFromView: self.myMap)

        var location = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)

        var loc = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

        CLGeocoder().reverseGeocodeLocation(loc, completionHandler: { (placemarks, error) -> Void in

            if error == nil {

                // Placemark data includes information such as the country, state, city, and street address associated with the specified coordinate.
                let placemark = CLPlacemark(placemark: placemarks[0] as! CLPlacemark)


                var subthoroughfare = placemark.subThoroughfare != nil ? placemark.subThoroughfare : ""

                var thoroughfare = placemark.thoroughfare != nil ? placemark.thoroughfare : ""

                var city = placemark.subAdministrativeArea != nil ? placemark.subAdministrativeArea : ""
                var state = placemark.administrativeArea != nil ? placemark.administrativeArea : ""

                var title = "\(subthoroughfare) \(thoroughfare)"
                var subTitle = "\(city),\(state)"

                //let annotation = MKPointAnnotation()
                    self.annotation.coordinate = location
                    self.annotation.title = title
                    self.annotation.subtitle = subTitle
                self.myMap.addAnnotation(self.annotation)
                println("Annotation Added")

            }

        })

    }

}
添加注释功能:

func removeAnnotation(gesture: UIGestureRecognizer) {

    if gesture.state == UIGestureRecognizerState.Ended {

        self.myMap.removeAnnotation(annotation)
        println("Annotation Removed")

    }

}
func addAnnotation(gesture: UIGestureRecognizer) {

    if gesture.state == UIGestureRecognizerState.Began {

        println("Long Press Started")

        var touch = gesture.locationInView(self.myMap)
        var coordinate = myMap.convertPoint(touch, toCoordinateFromView: self.myMap)

        var location = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)

        var loc = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

        CLGeocoder().reverseGeocodeLocation(loc, completionHandler: { (placemarks, error) -> Void in

            if error == nil {

                // Placemark data includes information such as the country, state, city, and street address associated with the specified coordinate.
                let placemark = CLPlacemark(placemark: placemarks[0] as! CLPlacemark)


                var subthoroughfare = placemark.subThoroughfare != nil ? placemark.subThoroughfare : ""

                var thoroughfare = placemark.thoroughfare != nil ? placemark.thoroughfare : ""

                var city = placemark.subAdministrativeArea != nil ? placemark.subAdministrativeArea : ""
                var state = placemark.administrativeArea != nil ? placemark.administrativeArea : ""

                var title = "\(subthoroughfare) \(thoroughfare)"
                var subTitle = "\(city),\(state)"

                //let annotation = MKPointAnnotation()
                    self.annotation.coordinate = location
                    self.annotation.title = title
                    self.annotation.subtitle = subTitle
                self.myMap.addAnnotation(self.annotation)
                println("Annotation Added")

            }

        })

    }

}
谢谢,,
杰克

我发现通过点击地图上的任何地方来删除注释是非常违反直觉的。弹出带有“删除”按钮的批注视图不是一种可以想象的方式吗?只需在批注上再添加一个TapSirture,就可以完成您的工作。@zisoft我看了谷歌地图,从他们那里得到了想法。@ViralSavaj我该怎么做?在tap Sirture处理程序中,您可以检查批注视图是否被点击并执行其他操作。参见目标C中的示例: