Tutorial 2 — GPS

Adding GPS functionality to the simplest mapping app

One of the most basic functions of a map is to show the user his/her current GPS location on the map. To do this in iOS, we need to use the CoreLocation library. We will add the CoreLocation GPS functionality to the app created in Tutorial 1 and then write a single line of mapping engine code so that the map follows the GPS location as it changes. Take the following steps.

Step 1

Modify ViewController.h so that it looks like this:

We are now importing CoreLocation.h, changing the @interface line and adding a locationManager property.

Step 2

Add these functions to ViewController.m:

Step 3

Add this line to the bottom of the viewDidLoad function in ViewController.m:

Step 4

Run the app. In the iOS Simulator menu bar, choose the Debug menu and then choose the Location item. Choose "Custom Location" and try changing the latitude and longitude. The map should pan to the new location so that the new location is centered on the screen.

Alternatively, if you click the small GPS arrow icon down by the console window in Xcode, and then if you select a city, the app will receive the location and the mapping engine will automatically pan to the new location. In addition, the emulator will call the didUpdateToLocation function approximately once per second (you can see this happening because there are NSLog lines being added to the console window). If you try to pan away from the city you have selected, the app will re-center on that city.

Step 5

This example is using the setCenterCoordinate method described in the documentation. If you do not wish to animate, you can replace that call with this line:

This is an alternative way to tell the mapping engine to put a specific lat/lon location at the center of the view.

Step 6

If you get lost or wish to save yourself the typing, simply checkout tutorial2 using these commands:

git reset --hard
git checkout tutorial2

Step 7

Many apps would like to use the GPS in Track-Up mode, where the map rotates as the vehicle changes direction. This functionality is explained in Tutorial 3.

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].