Creating GPX file from array of CLLocation

ぃ、小莉子 提交于 2019-12-05 03:35:51
Aleksandar Vacić

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

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.

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

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 = ""
  }
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!