How to let the user set a pin on your MKMapKit Map View

You know the Apple Map feature that lets you set a “Dropped Pin” on a Map View. I was wondering how they did it, and after a bit of research I found out:

It’s a clever combination of a long touch touch gesture recogniser, its “view-to-map-coordinate” method and your own class implementing the MKAnnotation protocol. It’s a bit complex so please bear with me.

Screen Shot 2014-03-20 at 15.56.03

Create an MKAnnotation Class

Let’s start from the end and work our way backwards: the MKAnnotation is an object that has the CLLocationCoordinate2D point of your map. This is the “red pin” you see on a map. Once you have it, you simply add it to your Map View.

Note that this object does not already exist which is a little weird – so we’ll have to create one first. I’ll call mine Pin and make it a subclass of NSObject. It needs to conform to the MKAnnotaion protocol, and as such it requires us to create three properties. Here’s the Pin.h file:

This content is for members only.

We add our own class as the target and choose the selector we’ve setup in the previous step. We want to wait half a second before touches are converted into pins. The last line adds our touch gesture to the Map View.

And that’s it!

I have a working demo of this on GitHub. It includes several other Map Kit related features – take a look:

Further Reading:

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

1 thought on “How to let the user set a pin on your MKMapKit Map View

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.