The Challenge

A client asked us to build an iOS app that uses geolocation to map routes. At any time during tracking, the user needs the ability to record different observations. Usually, mimicking a single GPS location is pretty straightforward, one would simply set the location in Xcode, but what about testing a route?

Fortunately, in Xcode there is a way to add a GPX file to simulate locations and I will demonstrate a way to generate the file easily in this article.

Here are the required tools:

Step 1: Google Maps

Go to Google Maps, enable directions, and enter a starting location and destination. A suggested route should be created on the map. Clicking and dragging on this route would create waypoints, just be aware that at this time Google Maps only allows a finite number of points (workaround for longer routes later), and it seems to have something to do with the URL length that it generates.

Google Maps Route

Once a route has been plotted, copy that URL.

Tip: If a longer route is needed, start a new map that continues on, or for a looping route, paste the URL into a new window, flip the locations, and plot a new route.

Google Maps Route

Step 2: GPX Conversion Tool

Open this tool in a new browser window, and paste the URL from the previous step. Make sure Track Points, GPX, and Create Waypoints are checked, then click “Let’s Go”. It will prompt you to download a GPX file. Simple right?

GPX Generation Tool

Step 3: Text Editor

There are a couple of manual manipulations required to complete a GPX file that will work in Xcode. Go ahead and open the generated GPX file in a text editor.

In this file, there will be a list of <trkpt> nodes, these are the waypoints created in step 1.

Editing GPX

Since Xcode uses <wpt> nodes (this GPX file should have two: start and end locations), let’s move these <trkpt> nodes between the existing <wpt> nodes. The first and last <trkpt> nodes are start and end locations, so these can be excluded. Then rename all <trkpt> nodes to <wpt> and save the file. Additional routes (see step 1) can be added to this in the same manner.

Editing GPX

Tip: Xcode also recognizes <time> nodes within <wpt>. If these are present, the geolocations will be simulated in sequence. This is useful when it comes to testing an actual route. Without the <time> nodes, the waypoints seem to be simulated in a pseudo-random fashion (i.e.: randomized but with same seed), which can be useful if the test required is only to plot a cluster of markers and order is irrelevant.

Step 4: Xcode

In Xcode, start the app in either a simulator or a device. Simulate location by loading the GPX file into the project.

Load GPX into Xcode

Now see the geolocations getting plotted in the app!

See route in app

Final Thoughts

While this method does take a bit of manual work, it saves a lot of time for either generating a file manually, using a GPS device or actually moving around. One drawback is that this does not provide information on speed and heading, which in some situations is necessary. For these scenarios, check out this post for a solution.