RPeck RPeck - 1 year ago 116
iOS Question

Using GPX route or track for testing in Xcode 4.2

Has anyone had any success using a GPX route or track to test location aware apps that depend on movement in Xcode 4.2? I have been able to get it to use a single waypoint in a GPX file or even get it to iterate over a series of waypoints, but I have not been able to have it follow a track in a way that would give speed and course information. I have tried with recorded tracks from driving, hand made routes and tracks, and routes made with Trailrunner.

It may be that this functionality just is not available, but Apple does provide a freeway drive in the simulator. I want to be able to do something similar on device and in a location that I can specify. Anybody have any thoughts?

Answer Source

The best way is to create your own simulator class to read the file and generate CLLocation events, which gives you complete control. GPX files don't contain speed and heading, so you'll have to calculate them yourself. The basic simulation loop I've used is:

  if (!eof) {
    Read the next waypoint
    Calculate distance, dt (delta time), bearing and speed (distance/dt) from the previous waypoint

    Generate a CLLocation object and call the didUpdateToLocation delegate method
    Peek at the next waypoint and calculate next_dt
    Scale next_dt   // as desired for faster playback (e.g. dt/2 would be 2x)
    Start an NSTimer with next_dt as the duration

When the timer fires, call Simulate again

Note: Until recently, synthesizing CLLocations was somewhat problematic since CLLocation is immutable and the init method had no way to set speed and heading. This was remedied in iOS 4.2.