iOS API Tutorials
Tutorial 9 — Touch To Add Marker
Creating an app that can handle markers created by the user
One of the most common use cases for a mapping app is to let the user drop markers on the map. In this tutorial, we will start with the app from Tutorial 1 and add markers to it.
Step 1
Let's first bring in the code for this tutorial and run it to see what this new app can do. If you have been working through all of the tutorials (if not please see Tutorial 1), type:
git checkout tutorial9
Run the app. Anywhere on the map where you tap, a marker will appear.
Step 2
In you look in ViewController.h, you will see two changes. This part is added to the ViewController interface:
There is also a new property called markerCounter to keep track of the number of markers. We will use this counter to help create unique names for each marker:
Step 3
Note that there are two new files in the project's Images directory in Xcode. These two files are named pinRed.png and [email protected]. You can change the appearance of your markers in any way that you like.
Step 4
The amount of code added to implement markers is very small:
The addMarkerLayer method creates a new dynamic marker layer with a zOrder of 5. The name of the layer is arbitrarily set to "Red Pin Markers" - you can name it anything you like as long as the name is unique.
The singleTapAt method gets called by the mapping engine whenever the user single-taps. Inside this function we allocate a new marker, set its location, give it a unique name and then add the marker to the map.
The updateMarkerInfo method gets called every time the mapping engine wants to display a marker. This might seem like overkill, but it allows you to style markers in any way that you like. For example, in the reference app, there is a TAWS tower example that changes marker color depending on the altitude reported by the GPS. In the example code in this tutorial (and in most cases in most apps) we simply return the same PNG file for every marker. However, you might want to color markers differently based on temperature, altitude or anything else.
Step 5
At the bottom of initializeMappingEngine add:
Step 6
At the bottom of viewDidLoad add:
Step 7
A common question here is, "How can I persist the markers?" You would need to do something in singleTapAt to keep a record of each marker. The easiest way is to use a data structure like NSMutableDictionary to store each marker's unique name and its lat/lon coordinate. This could be saved to disk and then read back in when needed, adding each marker to the mapping engine when the app starts. Another possibility is an sqlite database.
If you have a set of static markers, especially a large set (e.g. all of the convenience stores in the United States), the preferred method is to let METool build a marker layer containing all of the markers. The file that METool creates can be bundled with the app and will have excellent performance.
You can learn more about the Altus Mapping Engine™ classes used in this tutorial in the Documentation. If you have any questions about the Altus Mapping Engine™, any feature requests or suggestions for improving the Altus Mapping Engine™, please send them to [email protected].