This example demonstrates how UNL Core library can be used in an iOS app with Mapbox Maps SDK for iOS library to draw a UNL grid. In this example, we are going to generate the Unl grid lines and display them on the map by creating and adding a new layer to the map. In addition, we will add the option to show/hide this layer.
- Create a "Single view App" from XCode. Make sure to select the "Storyboard" option from the field "User interface":
- Open a new terminal window,
cdinto your project and run
pod initto generate a podfile.
If you don't have CocoaPods installed or want to read more on this topic, you can check out the official documentation.
- Install Mapbox Maps SDK for iOS
- Sign up to Mapbox and go to your Account page
- Create an access token
- Create a .netrc file with the following content:
Open your project's
Info.plistfile and add a
MGLMapboxAccessTokenkey whose value is your public access token.
Add the dependency to your
You can read the complete installation guide here.
- Install UnlCore
To add UnlCore as a dependency to your iOS project you must:
- Add the following dependency in the podfile of your project:
You can read more about the installation and usage of the Unl Core library here.
We will display the map at a default location and zoom:
We will restrict the zoom level at which the unl grid layer will be visible. Also define a color for the lines:
In order to display the grid lines, we need to create a new layer with an associated source once the map is loaded. We use the UnlCore library to generate the grid lines based on the visible map bounds:
Now whenever the map bounds change, we update the source with its new shape:
Add the functions for changing the grid lines layer visibility:
Add the toggle button: