Simulating A Moving Location In iOS
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):
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!