Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapVIew - Place Pin at tap point

I am trying to detect the location of a tap on a MKMapVIew to place a pin on that location. Is there an event I can capture which will give me this information?

like image 594
Ian Vink Avatar asked Oct 11 '11 15:10

Ian Vink


1 Answers

Here is the answer translated to MonoTouch in C#

Add this to the ViewController holding the MKMapView

public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        UILongPressGestureRecognizer sgr = new UILongPressGestureRecognizer ();
        sgr.AddTarget (this, new MonoTouch.ObjCRuntime.Selector ("LongPressGesture"));
        sgr.MinimumPressDuration = 1f;
        sgr.Delegate = new LongPressRecognizerDelegate ();
        this.View.AddGestureRecognizer (sgr);
    }

    [Export("LongPressGesture")]
    public void Handle (UIGestureRecognizer recognizer)
    {
        //http://freshmob.com.au/mapkit/mapkit-tap-and-hold-to-drop-a-pin-on-the-map/
        //and
        //http://inxunxa.wordpress.com/2011/03/10/monotouch-longpress/


        if (recognizer.State != UIGestureRecognizerState.Began)
            return;

        // get the point of the   action
        PointF point = recognizer.LocationInView (this.View);

        CLLocationCoordinate2D coord = this.map.ConvertPoint (point, this.map);
        //Add pin annoation here

        LocationAnnotation ann = new LocationAnnotation (new LocationEntity (coord));
        this.map.AddAnnotation (ann);
    }

    public class LongPressRecognizerDelegate : MonoTouch.UIKit.UIGestureRecognizerDelegate
    {
        public override bool ShouldReceiveTouch (UIGestureRecognizer recognizer, UITouch touch)
        {
            return true;
        }
    }
like image 56
Ian Vink Avatar answered Nov 07 '22 15:11

Ian Vink