Tutorial by Ranen Ghosh
This tutorial will show you how to track your location on the globe or map. The basic functionality is showing a pulsing marker at your location, with a direction arrow if heading is available. There’s also the ability to lock the screen on your location, and there are a few different ways of doing that.
You will need to have already run through the remote image layer tutorial. Let’s get started by opening your HelloEarth project.
If you haven’t got one here is a suitable ViewController file to start with (for Objective-C or Swift). This version is from the previous tutorial for a remote image layer.
In order for your app to get permission to access location services, you have to ask. In XCode, go into the settings for your project, and go to the Info tab. Under Custom iOS Target Properties, add an entry for “Privacy - Location When In Use…” The message you enter here will be displayed when the user’s prompted for location permission.
You can start tracking location with one simple command, but first you have to have an object implementing the MaplyLocationTrackerDelegate protocol. In our case, we’ll use the view controller class itself.
Change the class declaration to implement the protocol:
Then, implement the location event callbacks:
For now we’ll leave these empty, but they allow your app to respond to location events further up the line.
Now, call the method startLocationTrackingWithDelegate:useHeading:useCourse:simulate:
If you now run your app, you will see your current location marked on the map or globe. If heading or course are available, they will be indicated by an arrow on the marker.
The first argument is the delegate, which in our case is the view controller class. The useHeading argument is true if heading should be used when available. The useCourse argument is true if course, when available, should be used as a fallback if heading is unavailable. Leave the last argument false for now.
Now, let’s keep the screen locked on the current location:
In this example, as you travel the map will stay centered on your position.
In the previous example your position stayed at the center of the screen, with the map or globe oriented North-up.
There are actually several lock modes available:
Lock mode | Description |
MaplyLocationLockNone | Default behavior; screen doesn’t move with travel |
MaplyLocationLockNorthUp | Screen stays centered on user’s position, oriented North-up |
MaplyLocationLockHeadingUp | Screen stays centered on user’s position, oriented heading-up |
MaplyLocationLockHeadingUpOffset | User’s position is fixed to a point above or below the view’s center by an offset amount, oriented heading-up |
To use the MaplyLocationLockHeadingUpOffset locking mode, call the changeLocationTrackingLockType:forwardTrackOffset: method.
The offset amount is the number of pixels below or above the center at which to fix the user’s position. A positive value is below.
You can also simulate locations with headings. (The Apple development tools allow simulating position, but not heading).
In our example, declare a longitude variable amoung the class variables.
Initialize it in viewDidLoad, before the startLocationTracking… method call.
Then implement the MaplyLocationTrackerDelegate protocol’s getSimulationPoint method:
What this will do is simulate a track that starts at 120W, 49N and moves eastward over time. (Every time the location tracker calls your getSimulationPoint method, the longitude is moved slightly eastward.)
Now change your startLocationTracking… method call so that simulate is set to true:
The result in this example is a track moving eastward over the Canada-U.S. border.
Finally, to stop tracking location, simply call the stopLocationTracking method: