从CLLocation数组中创buildGPX文件
我的应用程序需要使用应用程序共享设备内的CLLocations(Route)数组。我之前没有使用过GPX的经验。 GPX是最好的格式吗? 我如何从给定的CLLocations数组创buildGPX文件? 目标C中是否有标准的GPXparsing器? 从我在网上search和所以这些问题的答案分别是
1.cant 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
我会很高兴,如果我得到替代答案/意见。 我明白这是很多问题。 任何帮助或build议将非常感激。
Watanabe Toshinori刚刚成为你的新朋友(我的也是),代码在这里:
http://github.com/FLCLjp/iOS-GPX-Framework
回答你的具体问题:
-
是的,GPX是共享位置数据的非常好的格式。 那是什么
-
我没有代码来做到这一点,但你将需要迭代你的CLLocations数组,并构buildXML数据结构。 使用一个支持写入的XMLparsing器。 (请参阅下面的链接)。
-
目标C中没有标准的GPXparsing器,但GPX只是xml,因此您可以使用在iOS或OSX下工作的任何xmlparsing器。 Ray Wenderlich 在iOS上使用xml有一个很好的教程,它解释了如何根据需要select正确的parsing器。 这不是GPX特有的,但GPX又是一个xml的味道; 原则是一样的。
GPX示例:
<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>
gpx(GPS eXchange Format)文件以.gpx扩展名结尾
您只需迭代CLLocations数组并创build标记(wpx)并将文件另存为.gpx
我希望这有帮助
这里是在迅速4,以防万一任何人用UIAlert需要input文件名
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 = "" } }
使用Xcode 5,添加新文件 – >资源 – > GPX文件。