How to simulate locations in Xcode

merlos
6 min readDec 7, 2019

--

Debug iOS applications that trigger actions based on user location is challenging. However, Xcode allows us to simulate locations either manually introducing the latitude and longitude coordinates or using GPX files.

We’ll explain both methods in this article. For the latter, generating the GPX file, we will use Open GPX Tracker, an open source app that is available on the app store ; its source code is also published on Github. — Disclaimer — I am part of the development team of this app :-).

Generally, we can work most of the time with fake data but when release is close, we want to test it with some real data and more real locations. So, let´s see how can we achieve this.

Enter location manually

The most straight forward way of simulating a location is entering the latitude and longitude manually.

We just launch our app and in the simulator and in the Simulator menu Debug / Location / Custom Location we enter the desired coordinates.

Simulator -> Debug/Location/Custom coordinates
Custom location Lat and long

Tip: You can easily get the coordinates of a point, using Open Street Maps. The URL uses the coordinates of the center of the map. With Google Maps you can also get the coordinates from the URL, just long press somewhere in the map and it will display the coords.

Also, note that the simulator offers the possibility of selecting City Run, City Bicycle Ride and Freeway Drive which will to simulate the user moving at different speeds in locations close to Apple’s Headquarters.

Options iOS simulator provides

Location simulation using GPX files

Using Custom location is ok if we do not need to do many tests, but it may be cumbersome. Every time we change the location, we may need to copy paste the latitude and longitudes or learnt them by heart. Also, it does not simulate the user moving from one location to another “naturally”

Luckily, Xcode offers the possibility of using GPX files. GPX is a XML file standard that stands for GPS Exchange Format. This file format is pretty simple and it is widely used in the GPS world.

A GPX file looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<gpx
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="Open GPX Tracker for iOS">
<wpt lat="40.762468446233115" lon="-73.99090283852468">
<time>
2019-12-06T14:19:25Z</time>
<name>
Point 1</name>
<desc>
Description of point 1</desc>
</wpt>
<wpt
lat="40.80559910750484" lon="-73.95810627601767">
<time>
2019-12-06T14:19:29Z</time>
<name>
Point 2</name>
<desc>
Description of point 2</desc>
</wpt>
<wpt
lat="40.767588954221175" lon="-73.9539347834181">
<time>
2019-12-06T14:19:31Z</time>
<name>
Point 3</name>
<desc>
Description of point 3</desc>
</wpt>
<wpt
lat="40.72268937145068" lon="-73.99262897339348">
<time>
2019-12-06T14:19:36Z</time>
<name>
Point 4</name>
<desc>
Description of point 4</desc>
</wpt>
</gpx>

This file is available as a Gist in Github.

The points of the GPX file above correspond with the pins of this image:

Shows the GPX points of the file above in a map of Manhattan (NY)
GPX points in the map (New York / Manhattan)

We will not dig much into the GPX file format the only thing that we need to know is that Xcode uses these waypoint elements <wpt/> that have two attributes lat and lon which respectively correspond to the latitude and longitude of the location to simulate the GPS coordinates.

<wpt lat="40.762468446233115" lon="-73.99090283852468"> 
<time>2019-12-06T14:19:25Z</time>
<name>Point 1</name>
<desc>Description of point 1</desc>
</wpt>

Xcode will simulate the movement of the device sequentially going from the first point to the second, from the second to the third, and so on so forth. Once it arrives to the last point of the GPX file it will go back to the first one.

XCode will move user location in sequential order

The time it will take to move the user location from one point to the next is determined by the difference of the <time>elements. In the example above, time for Point 1 is 2019-12-06T14:19:25Z and time for Point 2 is 2019-12-06T14:19:29Z, hence, there are 4 seconds of difference. Therefore, Xcode will use 4 seconds to go from Point 1 to Point 2. During this 4 seconds the location will change smoothly, which means that if you implemented locationManager(_:didUpdateLocations:) of CLLLocationManagerDelegate you will receive several updates during the 4 seconds XCode simulates Point 1 to Point 2.

Output of simulating in Xcode the GPX file in Open GPX Tracker GitHub Project

Setting up Xcode
First thing we need to do is to be sure that we have the option to simulate location enabled in Xcode. This comes by default enabled, but just in case…

  1. Click on the Active Scheme section.
Active Scheme

2. Select Edit Scheme

3. In the Run Scheme / Options tab, enable the Allow Location Simulation checkbox.

Generate the GPX file

Icon of Open GPX Tracker

As we mentioned earlier, to generate the GPX file we are going to use Open GPX Tracker (it works in iPhone and iPad). Alternatively, if you do not want to download the app you can use GPX-POI website.

Once you download the app:

  1. Open it :-).
  2. On the map, long press over the locations in which you want to simulate a location.

3. Press the Save button, to give the file a meaningful name.

4. Next, press the share button at the top (next to the cog).
This will open the standard share options of iOS. Use whichever is better for you.

Share button

Import into Xcode
Now that you have the file in your Mac:

  1. Launch Xcode and open your project
  2. Run your application (click the play button)
  3. Once the app is running, in the main menu of Xcode open Debug / Simulate Location / Add GPX File to Workspace.
Screenshot of XCode that displays the menu Debug/Simulate Location
Menu of XCode / Debug / Simulate Location /Add GPX File to Workspace

4. Select the GPX file you downloaded from Open GPX Tracker.

XCode / Debug / Simulate location after adding the GPX File

Now, if you open again Debug / Simulate Location / Add GPX File to Workspace. The GPX File will appear.

You can also select the location to simulate by clicking in the location arrow icon in the Debug area of Xcode.

Limitations of location simulation

There are a few limitations when simulating locations using GPX files:

  1. No valid speed. CLLlocationinstances provided by CLLLocationManager do not provide valid a value in the speedproperty while using GPX files.
    This can be easily solved if we keep in a variable the previous location and the time we got it. Then when we get a new location, we calculate the speed using the differences of distances and times between the old and new location (speed = diff distances/ diff times).
  2. No valid elevation. As well as speed, altitudeis not provided even if you set the <elevation>element in the GPX file.
  3. No signal accuracy control. You have no control over the simulated horizontalAccuracyand verticalAccuracy.

Conclusion

Work with location enabled applications includes some additional challenges during debug time, however Xcode provides a couple of build-in tools tools that helps us: manually setting the custom location and the use of GPX files.

If you liked this article share it and give it some claps 👏🏽👏🏽👏🏽👏🏽👏🏽👏🏽👏🏽👏🏽

If you are building an application that uses your custom map tiles or tiles of Open Street Maps, you may like to take a look at MapCache swift library which allows you to cache the map as the user browses or to pre download whole regions of a map.

--

--

merlos

Dreamer, mobile and web geek, eternal learner. MBA from ISB (India). Citizen of the world made in Spain.