so in other question asked, found out can create gpx files, need display content of gpx file mkpolygon. before, had created list coordinates in plist file, easy read since make nsdictionary , read there , finding location using keys plist supplies, doesn't seem work easy in gpx file.
i've created little snipped of code read whole content of gpx file:
if filemanager.fileexistsatpath(filepath) { let databuffer = nsdata(contentsoffile: filepath) let datastring = nsstring(data: databuffer!, encoding: nsutf8stringencoding) print (datastring) }
so have whole text in string don't need this:
<?xml version="1.0" encoding="utf-8"?> <trk> <name>test</name> <desc>length: 1.339 km (0.832 mi)</desc> <trkseg> <trkpt lat="-39.2505337" lon="-71.8418312"></trkpt> <trkpt lat="-39.2507414" lon="-71.8420136"></trkpt> </trkseg> </trk> </gpx>
i need latitudes , longitudes between <trkpt>
tags can convert them locations , there convert mkpolygon.
any appreciated haven't found in google on how read gpx files swift.
thanks in advance -jorge
ok able read gpx file following code:
import foundation import mapkit //nsxmlparserdelegate needed parsing gpx files , nsobject needed nsxmlparserdelegate class trackdrawer: nsobject, nsxmlparserdelegate { //all filenames checked , if found , if it's gpx file generate polygon var filenames: [string]! = [string]() init(filenames: [string]) { self.filenames = filenames } //needs global variable due parser function can't return value private var boundaries = [cllocationcoordinate2d]() //create polygon each string there in filenames func getpolygons() -> [mkpolygon]? { //the list returned var polylist: [mkpolygon] = [mkpolygon]() filename in filenames! { //reset list won't have points previous polygon boundaries = [cllocationcoordinate2d]() //convert filename computer readable filepath let filepath = getfilepath(filename) if filepath == nil { print ("file \"\(filename).gpx\" not exist in project. please make sure imported file , dont have spelling errors") continue } //setup parser , initialize filepath's data let data = nsdata(contentsoffile: filepath!) let parser = nsxmlparser(data: data!) parser.delegate = self //parse data, here file read let success = parser.parse() //log error if parsing failed if !success { print ("failed parse following file: \(filename).gpx") } //create polygon points generated parsing process polylist.append(mkpolygon(coordinates: &boundaries, count: boundaries.count)) } return polylist } func getfilepath(filename: string) -> string? { //generate computer readable path return nsbundle.mainbundle().pathforresource(filename, oftype: "gpx") } func parser(parser: nsxmlparser, didstartelement elementname: string, namespaceuri: string?, qualifiedname qname: string?, attributes attributedict: [string : string]) { //only check lines have <trkpt> or <wpt> tag. other lines don't have coordinates , don't interest if elementname == "trkpt" || elementname == "wpt" { //create world map coordinate file let lat = attributedict["lat"]! let lon = attributedict["lon"]! boundaries.append(cllocationcoordinate2dmake(cllocationdegrees(lat)!, cllocationdegrees(lon)!)) } } }
i hope helps someone
Comments
Post a Comment