Simulating A Moving Location In iOS

Let's see how we can simulate a changing user's location with Xcode and the iOS Simulator.

While I was working at Porsche, I spent most of my time working on a new Navigation SDK. In order to successfully develop the SDK, we needed a way of simulating a user's changing location. In particular, we wanted to assess the performance of different navigation algorithms by testing them against the same set of location updates.

Fortunately, we can easily replay location updates using .gpx files in Xcode.

A .gpx file is essentially a mapping of GPS points over time; specifying a timestamp with each GPS coordinate allows you to simulate walking, biking, or driving along the highway.

Here's an example of what a .gpx file could like like (courtesy of MapBox):

<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="Garmin Connect"
  xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd"
  xmlns="http://www.topografix.com/GPX/1/1"
  xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
  xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <metadata>
    <link href="connect.garmin.com">
      <text>Garmin Connect</text>
    </link>
    <time>2012-10-24T23:22:51.000Z</time>
  </metadata>
  <trk>
    <name>Untitled</name>
    <trkseg>
      <trkpt lon="-77.02016168273985" lat="38.92747367732227">
        <ele>25.600000381469727</ele>
        <time>2012-10-24T23:29:40.000Z</time>
        <extensions>
          <gpxtpx:TrackPointExtension>
            <gpxtpx:hr>130</gpxtpx:hr>
          </gpxtpx:TrackPointExtension>
        </extensions>
      </trkpt>
      <trkpt lon="-77.02014584094286" lat="38.927609380334616">
        <ele>35.599998474121094</ele>
        <time>2012-10-24T23:30:00.000Z</time>
        <extensions>
          <gpxtpx:TrackPointExtension>
            <gpxtpx:hr>134</gpxtpx:hr>
          </gpxtpx:TrackPointExtension>
        </extensions>
      </trkpt>
Notice the mapping between GPS positions and time

We can create our own GPX trace using GPX Generator:

As you can see, this tool also allows us to specify the speed, which is a great convenience to have. Plus, this tool is entirely free :)

Here's what the downloaded .gpx file looks like:

<?xml version="1.0"?>
<gpx version="1.1" creator="gpxgenerator.com">
<wpt lat="37.77849246241238" lon="-122.41817443121337">
    <ele>18.00</ele>
    <time>2022-03-24T01:46:01Z</time>
</wpt>
<wpt lat="37.77866280032752" lon="-122.41660999231262">
    <ele>18.15</ele>
    <time>2022-03-24T01:46:21Z</time>
</wpt>
<wpt lat="37.77894066029837" lon="-122.41459250450134">
    <ele>15.51</ele>
    <time>2022-03-24T01:46:47Z</time>
</wpt>
<wpt lat="37.77870772846182" lon="-122.41474617094659">
    <ele>15.86</ele>
    <time>2022-03-24T01:46:51Z</time>
</wpt>
<wpt lat="37.77752053479124" lon="-122.4132548627343">
    <ele>12.92</ele>
    <time>2022-03-24T01:47:18Z</time>
</wpt>
<wpt lat="37.776260833361775" lon="-122.41163804122785">
    <ele>8.80</ele>
    <time>2022-03-24T01:47:47Z</time>
</wpt>
<wpt lat="37.775319528789794" lon="-122.41050078460553">
    <ele>7.89</ele>
    <time>2022-03-24T01:48:08Z</time>
</wpt>
<wpt lat="37.775007006940626" lon="-122.41008235999921">
    <ele>7.54</ele>
    <time>2022-03-24T01:48:15Z</time>
</wpt>
<wpt lat="37.773802792942675" lon="-122.41163804122785">
    <ele>10.46</ele>
    <time>2022-03-24T01:48:43Z</time>
</wpt>
<wpt lat="37.77624512200896" lon="-122.414727946013">
    <ele>15.70</ele>
    <time>2022-03-24T01:49:40Z</time>
</wpt>
<wpt lat="37.77755010903112" lon="-122.4164929111987">
    <ele>17.23</ele>
    <time>2022-03-24T01:50:11Z</time>
</wpt>
<wpt lat="37.77750770892152" lon="-122.41807005009946">
    <ele>16.81</ele>
    <time>2022-03-24T01:50:31Z</time>
</wpt>
<wpt lat="37.778465945462955" lon="-122.41826316914853">
    <ele>17.98</ele>
    <time>2022-03-24T01:50:47Z</time>
</wpt>
</gpx>

All we need to do now is point Xcode to the .gpx file so that every time we launch the Simulator, these coordinates will be repeated in a loop.

In your Xcode project, select your Target -> Edit Scheme. Then, in the Run -> Options tab, select the Default Location dropdown and choose "Add GPS Exchange to Project":

Now, you can pick the new .gpx file you just created.

Now, go ahead and launch your project.

Voila! The location of our user is now being simulated exactly following the custom path we provided.

If you're interested in more articles about iOS Development & Swift, check out my YouTube channel or follow me on Twitter.

If you want to be notified whenever I post a new article, join the mailing list below.


Do you have an iOS Interview coming up?

Check out my book Ace The iOS Interview!


Subscribe to Digital Bunker

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe