9

My application needs to share array of CLLocations (Route) within devices using application.I have no experience of using GPX before this. Is GPX is best format to do it? How can I create GPX file from given such array of CLLocations? and is there standard GPX parser in Objective C? From what I have searched on net and SO answer to these questions are respectively

  1. Can't say
  2. I have seen some webpages converting data of points in GPX format but could not find how they are doing it.
  3. No

I will be happy if I get alternate answers/views. I understand that these are lot of questions. Any help or suggestion will be hugely appreciated.

Display Name
  • 4,502
  • 2
  • 47
  • 63
chatur
  • 2,365
  • 4
  • 24
  • 38
  • I don't believe the GPX file is read by your app, more by the Simulator itself to feed locations into the app. There is a location arrow in Xcode you can click to load up your GPX file. There is also some default locations too you could probably look at too. – Bill Burgess Jan 03 '12 at 14:07

5 Answers5

7

Watanabe Toshinori just became your new best friend (mine as well), code is here:

http://github.com/FLCLjp/iOS-GPX-Framework

http://github.com/FLCLjp/GPX-Logger

http://github.com/FLCLjp/GPX-Viewer

Can
  • 8,502
  • 48
  • 57
Aleksandar Vacić
  • 4,433
  • 35
  • 35
  • First link in answer seems outdated. – Pang Oct 01 '15 at 09:24
  • 1
    Yep. The github project is also long abandoned, but the fork network shows a lot of promising forks to look into. Particularly: https://github.com/webventil/iOS-GPX-Framework and https://github.com/kimar/iOS-GPX-Framework – Aleksandar Vacić Oct 03 '15 at 18:19
5

Here it is in swift 4 just in case anyone needs it with a UIAlert to enter the filename

    func handleCancel(alertView: UIAlertAction!)
    {
        print("Cancelled !!")
    }

    let alert = UIAlertController(title: "Export GPX", message: "Enter a name for the file", preferredStyle: .alert)

    alert.addTextField { (textField) in
        textField.text = ""
    }
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:handleCancel))
    alert.addAction(UIAlertAction(title: "Done", style: .default, handler:{ (UIAlertAction) in
        if alert.textFields?[0].text != nil {
            let fileName = "\(String(describing: alert.textFields![0].text!)).GPX"
            let path = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
            var gpxText : String = String("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gpx version=\"1.1\" creator=\"yourAppNameHere\" xmlns=\"http://www.topografix.com/GPX/1/1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:gte=\"http://www.gpstrackeditor.com/xmlschemas/General/1\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">")
            gpxText.append("<trk><trkseg>")
            for locations in self.locationsArray{
                let newLine : String = String("<trkpt lat=\"\(String(format:"%.6f", locations.locLatitude))\" lon=\"\(String(format:"%.6f", locations.locLongitude))\"><ele>\(locations.locAltitude)</ele><time>\(String(describing: locations.locTimestamp!))</time></trkpt>")
            gpxText.append(contentsOf: newLine)
            }
            gpxText.append("</trkseg></trk></gpx>")
            do {
                try gpxText.write(to: path!, atomically: true, encoding: String.Encoding.utf8)

                let vc = UIActivityViewController(activityItems: [path!], applicationActivities: [])

                self.present(vc, animated: true, completion: nil)

            } catch {

                print("Failed to create file")
                print("\(error)")
            }

        } else {
            print("There is no data to export")

        }


    }))
    self.present(alert, animated: true, completion: {
        print("completion block")
    })
}


func textFieldHandler(textField: UITextField!)
{
      if (textField) != nil {
    textField.text = ""
  }
}
Rob.R
  • 481
  • 5
  • 11
3

GPX Sample:

<gpx xmlns="http://www.topografix.com/GPX/1/1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
    version="1.1"
    creator="YourCompanyName">

     <wpt lat="Your latitude" lon="Your longitude">

       <time>Your Time</time>

       <name>Your Location name.</name>

    </wpt>

</gpx>

The gpx(GPS eXchange Format) file ends with .gpx extension

All you have to do is just iterate the array of CLLocations and create the tags(wpx) and save the file as .gpx

I hope this helps

Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
3

To answer your specific questions:

  1. Yes, GPX is a very good format for sharing location data. Thats what its for.

  2. I don't have code to do this but you will need to iterate over your array of CLLocations and construct the xml data structure as you go. Use one the xml parsers that supports writing. (See the link below).

  3. There isn't standard GPX parser in Objective C, but GPX is just xml so you can use any xml parser that work under iOS or OSX. Ray Wenderlich has a very good tutorial on on using xml under iOS which explains how to choose the right parser for your needs. This is not GPX-specific but again GPX is just a flavor of xml; the principles are the same.

Dave Robertson
  • 431
  • 3
  • 6
  • Thanks a lot. That was helpful. another small question though, have you used GeoJSON? how its performance compared with GPX? – chatur Jan 06 '12 at 09:29
  • 2
    Performance is whole other question. I would stick with plain GPX unless your files are so big that performance is a problem. Note also that GPX supports GPS concepts like Tracks, Routes and Waypoints, but GeoJSON is more GIS-like and uses Points, Linestrings, Polygons, etc. You could use either format, but if you are representing routes then GPX may be a more natural fit for your application – Dave Robertson Jan 06 '12 at 09:42
  • 1
    Update: There is now an open source library for parsing GPX under iOS, the [iOS GPX Framework](http://gpxframework.com/). The source is on [Watanabe Toshinori's github site](https://github.com/FLCLjp) along with a KML library and example apps showing how to use the libraries. – Dave Robertson Jun 21 '12 at 04:36
  • 1
    how to write runtime user location lat long in GPX file my goal is i want to share user travelled route with another user – Darshan Jun 24 '16 at 11:03
-2

With Xcode 5 , Add New File - > Resource - > GPX File .

Ezra
  • 45
  • 1
  • 1
    OP was asking how to create a custom GPX file from an in-memory list of CLLocation points, not how to create a GPX file manually in Xcode. – worthbak Jan 03 '17 at 20:40