1

I have a MapView that allows me to use a UILongPressGestureRecognizer to add an annotation, with it's information, to the map. I then added a UITapGestureRecognizer that I want to remove the annotation when the user clicks on the map. It works great!

The issue I'm having is when I click on the pin, it removes it as well. I wanna be able to click on the pin to the show it's information, but then be able to click on the map view and remove the 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)


}

Remove Annotation Function:

func removeAnnotation(gesture: UIGestureRecognizer) {

    if gesture.state == UIGestureRecognizerState.Ended {

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

    }

}

Add Annotation Function:

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

            }

        })

    }

}

Thank You, Jack

JackB03
  • 55
  • 2
  • 7
  • I find it pretty counter-intuitive to remove an annotation by tapping anywhere on the map. Is popping up an annotation view with a delete button not an imaginable way? – zisoft Apr 18 '15 at 17:50
  • Just add one more TapGesture on Annotation, will make your job done. – Viral Savaj Apr 18 '15 at 17:57
  • @zisoft I looked at Google Maps and got the idea from them. – JackB03 Apr 18 '15 at 18:00
  • @ViralSavaj How would I do that? – JackB03 Apr 18 '15 at 18:01
  • In the tap gesture handler you can check if an annotation view was tapped and do something different. See this example in Objective-C: http://stackoverflow.com/questions/13498636/how-to-add-touch-gesture-to-map-but-ignore-touches-on-pins-and-annotations –  Apr 18 '15 at 18:58
  • @Anna I'm not to familiar with objective-c but I'll give it a shot – JackB03 Apr 18 '15 at 21:06
  • I still can't figure it out. Im not good with Objective-C. Can you tell me when I need to do with the .hitTest ? – JackB03 Apr 20 '15 at 05:16

0 Answers0