Where am I? (Learning Mapkit)

iDev
5 min readDec 22, 2020

This week was all about working with Mapkit. We are all familiar with maps when we want to know where we are or to get directions somewhere. Here are a couple of things I want to explain that I learned from using Mapkit this week and how you could learn the same by following along.

The first thing we need to do is import the framework into our project. This gives us the ability to work with all the things necessary to build a map.

MKMapView

The MKMapView is the actual visual representation of everything on the map. It is the map interface that you interact with and also shows you what you have coded. This is where you zoom in or drop pins which I’ll touch on later.

The default display of MKMapView

But where am I? How do I see on the map where I am? You know, that blue dot we are all familiar with. Well, first we need to ask for permission.

Get Permission For Location

Within the operating system of the phone(iOS), you can’t obtain the location of the user without first asking the user and getting authorization. This is done in the info.plist of your project. You may have seen this whenever you open a new app. It usually will ask for permission from the user if it will need to use certain data, such as the camera, camera roll, location, microphone, etc…

There is normally a pop-up alert that you can either grant or deny access to. To ask for permission and customize what the alert says you need to follow these steps.

  1. Go to the info.plist file in your project, and where it says Information Property List, click on the “+”.

2. Then a list should up, scroll down to “Privacy- Location When In Use Usage Description”.

3. Then, in the Value column, type the message you show to the user when the prompt is displayed.

4. Go to your view controller file and import another framework, “Core Location”. Then create an instance of CLLocationManager.

5. Finally, in the viewDidLoad, request the “when in use authorization” function from the location manager.

Now you have successfully asked for the location when in use.

But now how do we get that location to appear on our map? Very simply actually. First, a couple of things need to be in place. Make sure you connect an IBOultet to your MapView. Then make sure your view controller conforms to MKMapViewDelegate. Don't forget to set the delegate to self in the viewDidLoad. It should look like this.

Let's set a fake location of the project to New York. (There are many ways to do this) Click on the scheme board button, then a drop-down should appear. Then click edit scheme.

In default location, set to New York.

Ok, all we have to do now in the viewDidLoad function is set the “show user location” to “true” and that's it!

Run the app, Blue Dot!!

It seems a little zoomed out, right? Let’s get closer. To get closer we have to set a new region of how much we want to show. For sake of time here is everything needed to set and show the new region.

Zoomed in 500 meters by 500 meters(New region)

Next, Let's drop a pin.

Dropping a Pin

In Mapkit this is called an annotation. A couple of things are needed to drop a pin.

For annotations, we are going to make a class that indicates the spot and annotation. So the properties will hold the coordinates of the pin and the titles of the annotation, like so.

Make sure you make a variable with an optional MarkSpot.

Next, we are going to create a function that adds an annotation to the mapview from the class and variable we just created.

Don't forget, like it did, to make sure the CLLocationManagerDelegate is adopted and set the delegate to self in the viewDidLoad.

Then we make two functions to set up and handle a long-press on the screen. This will recognize where we long-press is and then we can set an annotation there. Make sure the handle long-press function is an Objective-C one.

Now, where configure the view of the annotation like animations or the color of the annotation.

Finally, we just call the setupLongPress function in the viewDidLoad and that's it!

I hope this was insightful on how to make a location appear and set an annotation. There was more I learned this week but this should be good for now. To see the production of this project, check out the timelapse I made of it. (https://www.youtube.com/watch?v=GFatflPDpWs)

Rick Martinez

--

--