Swiftpack.co - Package - vincentneo/CoreGPX


Parse, generate GPX files on iOS, watchOS & macOS.

CI Status Swift Version GPX Version License Platform Version Carthage compatible Swift Package Manager compatible

What is CoreGPX?

CoreGPX is a port of iOS-GPX-Framework to Swift language. It aims to be more than a port of the abandoned project, so do expect more features to be added in the future, as development is currently under progress.

It makes use of XMLParser for parsing GPX files, thus making it fully dependent on the native APIs only.


  • [x] Successfully outputs string that can be packaged into a GPX file
  • [x] Parses GPX files using native XMLParser
  • [x] Support for macOS & watchOS


CoreGPX works with CocoaPods. To install it, simply add the following line to your Podfile:

pod 'CoreGPX'

CoreGPX works with Carthage as well. To install via Carthage, simply add the following line to your Cartfile:

github "vincentneo/CoreGPX"

How to parse?

Parsing of GPX files is done by initializing GPXParser.

There are five ways of initializing GPXParser, and these are three main ways of initializing:

You can initialize with a URL:

guard let gpx = GPXParser(withURL: inputURL)?.parsedData() else { return }

With path:

guard let gpx = GPXParser(withPath: inputPath)?.parsedData() else { return } // String type

With Data:

let gpx = GPXParser(withData: inputData).parsedData()

.parsedData() returns a GPXRoot type, which contains all the metadata, waypoints, tracks, routes, and extensions(if any), which you can expect from a GPX file, depending on what that file contains.

Making use of parsed GPX data

guard let gpx = GPXParser(withURL: inputURL)?.parsedData() else { return // do things here when failed }
// waypoints, tracks, tracksegements, trackpoints are all stored as Array depends on the amount stored in the GPX file.
for waypoint in gpx.waypoints {  // for loop example, every waypoint is written
    print(waypoint.latitude)     // prints every waypoint's latitude, etc: 1.3521, as a Double object
    print(waypoint.longitude)    // prints every waypoint's latitude, etc: 103.8198, as a Double object
    print(waypoint.time)         // prints every waypoint's date, as a Date object
    print(waypoint.name)         // prints every waypoint's name, as a String
    print(gpx.metadata?.desc)    // prints description given in GPX file metadata tag
    print(gpx.metadata?.name)    // prints name given in GPX file metadata tag

How to create?

You will first start off with a GPXRoot.

Initializing GPXRoot

let root = GPXRoot(creator: "Your app name here!") // insert your app name here

Now, you can start adding things to your GPXRoot. This includes your metadata, waypoints, tracks, routes, as well as extensions(if any).

Adding waypoints to GPXRoot

root.add(waypoints: arrayOfWaypoints) // adds an array of waypoints
root.add(waypoint: singleWaypoint)    // adds a single waypoint

Adding tracks to GPXRoot

root.add(tracks: arrayOfTracks)       // adds an array of tracks
root.add(track: singleTrack)          // adds a single track

Adding routes to GPXRoot

root.add(routes: arrayOfRoutes)       // adds an array of routes
root.add(route: singleRoute)          // adds a single route

Adding metadata to GPXRoot

let metadata = GPXMetadata()
metadata.name = "Your Name Here"
metadata.desc = "Description of your GPX file"
root.metadata = metadata              // adds metadata stuff

Example of application of GPXRoot

let root = GPXRoot(creator: "Your app name here!")
let trackpoints = [GPXTrackPoint]()

let yourLatitudeHere: CLLocationDegrees = 1.3521
let yourLongitudeHere: CLLocationDegrees = 103.8198
let yourElevationValue: Double = 10.724

let trackpoint = GPXTrackPoint(latitude: yourLatitudeHere, longitude: yourLongitudeHere)
trackpoint.elevation = yourElevationValue
trackpoint.time = Date() // set time to current date

let track = GPXTrack()                          // inits a track
let tracksegment = GPXTrackSegment()            // inits a tracksegment
tracksegment.add(trackpoints: trackpoints)      // adds an array of trackpoints to a track segment
track.add(trackSegment: tracksegment)           // adds a track segment to a track
root.add(track: track)                          // adds a track
self.gpxString = root.gpx()

This would be what you get from root.gpx() in the above example:

<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="Your app name here!">
			<trkpt lat="1.352100" lon="103.819800">
  • .gpx() of GPXRoot outputs a String which can then be packaged as a .GPX file.
  • .OutputToFile(saveAt:fileName:) directly saves GPX contents to a URL specified.


To know in depth of what CoreGPX can bring, do check out the Example app. To run the example project, simply clone the repo, and try it out straight away!


Contributions to this project will be more than welcomed. Feel free to add a pull request or open an issue. If you require a feature that has yet to be available, do open an issue, describing why and what the feature could bring and how it would help you!

Currently Work in progress:

Any help would be appreciated!

  • [ ] Extension to metadata to support collection of more info in GPX file
  • [ ] Documentation
  • [ ] Better code optimisation
  • [ ] New features


CoreGPX is available under the MIT license. See the LICENSE file for more info.


Stars: 3
Help us keep the lights on


Used By

Total: 0


0.4.4 - Feb 27, 2019

This update includes:

  • a bug fix where href of GPXLink will produce null every time when generating a GPX file.

Special thanks to @jdleung for spotting and informing regards to this bug!

0.4.3 - Feb 20, 2019

While this update isn't all that big, it still includes these features:

  • Added Tests
  • Added a direct way of saving a GPX file from GPXRoot
  • Fixed missing Xcode example project in workspace
  • Removal of CGFloat related methods in GPXType (Apple Watch doesn't support CGFloat and the whole project uses Double now anyways)

0.4.2 - Feb 15, 2019

Just some new init methods in GPXParser, and other minor changes including:

  • switching some init options to convenience inits
  • new init options
  • parser in GPXParser is now immutable

0.4.1 - Feb 12, 2019

Minor updates, mostly focused on the GPXParser class.

#18 is now done, which is the main focus of this update.

  • bounds, author, copyright and email tags are now parsed by the parser.
  • new init method for GPXLink (with href:)

0.4.0 - Feb 10, 2019

This update fixes bugs, as well as speed improvements to the GPX file parser.

This includes:

  • Speed improvement through optimisation in GPXParser.
  • Fixed bug where a file with more than one track segment will not work as expected.
  • Added support for macOS and watchOS.
  • Partial support of parsing metadata tag in GPX files.
  • When no extensions are defined, the extensions tag is no longer written, as it is meant to be empty.
  • Code cleanup

🎉Special Thanks to @lludo for cleaning up the code!