在MKMapView Swift中删除Pin注释问题

时间:2015-04-18 17:29:41

标签: ios swift mkmapview uigesturerecognizer uitapgesturerecognizer

我有一个MapView,允许我使用UILongPressGestureRecognizer向地图添加注释及其信息。然后我添加了一个UITapGestureRecognizer,我想在用户点击地图时删除注释。它很棒!

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

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

            }

        })

    }

}

谢谢你, 千斤顶

0 个答案:

没有答案