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 in this article I will demonstrate a way to generate the file easily.
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 (work-around for longer routes later), and it seems to have something to do with the URL length that it generates.
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.
Step 2: GPX Conversion Tool
Open this tool in a new browser window, and paste the URL from 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?
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.
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.
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.
Now see the geolocations getting plotted in the app!
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.