Using Your Custom Mapbox Map and Adding Navigation Using Swift
Tags: swift, ios, mapbox, exploreoutdoors2018
Published: 2018-09-03 04:10:17

---
title: Using Your Custom Mapbox Map and Adding Navigation Using Swift
published: true
description: Using Your Custom Map In a Swift App (Part 2)
tags: swift, ios, mapbox, exploreoutdoors2018
---

In the [last post we created a custom map using Mapbox Studio](https://dev.to/rockarts/building-a-custom-map-using-mapbox-studio-to-add-to-your-apps-3oi6 ), in this blog post we will use that custom made map in an iOS app written in Swift and add some Navigation to it.

If you would prefer to watch a screencast about this instead you can view it below.

{% youtube YKjbJqp4Wbc %}

First we need to create a new project in XCode. Go to File->New->Project (Single View App) and name your project ExploreOutdoors. Now in the directory you saved the project in we will need to create a PodFile so that we can install the Mapbox and Mapbox Navigation apis. My directory layout looks like this:

![Directory layout](https://thepracticaldev.s3.amazonaws.com/i/2z9bnxtp19y48fa3lwr8.png)

Create a file named PodFile and add the following code.

{% gist https://gist.github.com/rockarts/109a8b427a64c2d76aed8c42c344c0e4 %}

Open up a terminal and run pod install to install the Mapbox and Navigation API.

![Pod install](https://thepracticaldev.s3.amazonaws.com/i/g0m1h2du3v077riwl6hq.png)

**Now, make sure to close XCode and open up ExploreOutdoors.xcworkspace and not ExploreOutdoors.xcodeproj make sure you can build and run your app with the Mapbox pods installed.**

If everything is working, in ViewController.swift add the following code:

{% gist https://gist.github.com/rockarts/9f5baca9f0a813aaa07461e0a167e6f3 %}

If you build and run your app right now it won't work, we need to add some entries to our Info.plist file. Lets add our mapbox key, you can get this by visiting the following URL: [https://www.mapbox.com/install/ios/cocoapods-permission/](https://www.mapbox.com/install/ios/cocoapods-permission/) they even have a handy animated gif on how to add it.

Next while you are in the Info.plist we need to add 2 more entries. "Required background modes" and for the value type in audio. Also add in "Privacy - Location When In Use Usage Description" and tell the user that we need access to their location so we can show them on the map. 

![Infoplist entries](https://thepracticaldev.s3.amazonaws.com/i/jsh4p9hgitbhgewsymmn.png)

Now for the cool part! We will add our custom built map from the last blog post. Go to https://www.mapbox.com/studio/ and select the share and use button. Select iOS on the side and copy your style URL. Paste it underneath the comment that says  //PASTE YOUR STYLE URL IN THE STRING!

Now you should be able to launch your app and see your custom maps being displayed! Check out my custom map of rock climbs in Jasper National Park!

![rock climbs](https://thepracticaldev.s3.amazonaws.com/i/8dcg4wijs1urkp5sg4a3.png)

##Add Some Navigation

Now let's add navigation to the app. We are going to make it so the user can tap on the screen to add navigation points. For that add these two lines above your viewDidLoad() method:

var button: UIButton!
var points = [CLLocationCoordinate2D]()

Now we will add a gesture recognizer to the map to recognize when the user taps on the map. Add the following code to your ViewController. 

{% gist https://gist.github.com/rockarts/f573014a830ac7601f63192ee73d4d31 %}

You should now be able to add annotations to your app by tapping the screen! 

![Adding annotations](https://thepracticaldev.s3.amazonaws.com/i/9hvl0p2zfc169zavcbe0.gif)

Let's explain the code step by step:

This code sets up a single tap gesture recognizer, binds it to the method handleTap and then adds it to the mapview.

{% gist https://gist.github.com/rockarts/eed04e8ac42ee2bdc1eebef666fc4944 %}

Now this code is really cool, it gets the point on the map where the user tapped, converts it from a point into a GPS coordinate and then creates an annotation and adds it to the map!

{% gist https://gist.github.com/rockarts/80a59b2fcb9a935c425ae1444b0b135f %}

Alright, now once the user has added 2 points to the map we can give them navigation instructions. 

Let's add a button to the screen in our viewDidLoad so that the user can trigger starting the navigation.

{% gist https://gist.github.com/rockarts/5394d059f433b4e686278efffdb9483c %}

One thing to remember when adding buttons to mapviews, is that if you want them to appear above the map, you need to add them to the map subview after it has been added to the parent view.

Now let's handle the tap and fetch the Navigation instructions from Mapbox. Add this code to the ViewController.

{% gist https://gist.github.com/rockarts/fc83c0c338ca738b0dc84c55f772b0e6 %}

This code looks at our points and sets the options to .automobile for directions, then using those waypoints it calculates the best route and presents the NavigationViewController from Mapbox. One last modification we need to do is enable the navigation button if there are more than 2 points on the map by adding a couple lines to our handleMapTap methond

{% gist https://gist.github.com/rockarts/a2daf3a6f87c834126083454aa37972e %}

You should now be able to navigate in your app! I've posted the finished code at the bottom of this post. Let me know if you have any questions!

![Adding annotations](https://thepracticaldev.s3.amazonaws.com/i/0zggu141ynl3nuwshfkg.gif)

##Finished Code

{% gist https://gist.github.com/rockarts/5406131a54b8226f3ed94005813f1a6d %}