9

我的应用程序需要使用应用程序在设备内共享 CLLocations(路由)数组。在此之前我没有使用 GPX 的经验。GPX 是最好的格式吗?如何从给定的 CLLocations 数组创建 GPX 文件?Objective C 中是否有标准的 GPX 解析器?根据我在网上搜索的内容,这些问题的答案分别是

  1. 不能说
  2. 我已经看到一些网页以 GPX 格式转换点的数据,但找不到他们是如何做到的。

如果我得到其他答案/观点,我会很高兴。我知道这些问题很多。任何帮助或建议将不胜感激。

4

5 回答 5

7

Watanabe Toshinori 刚刚成为你最好的新朋友(也是我的朋友),代码在这里:

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

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

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

于 2012-04-30T11:00:14.140 回答
5

这里是 swift 4,以防万一有人需要使用 UIAlert 输入文件名

    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 = ""
  }
}
于 2017-12-18T01:46:08.873 回答
3

要回答您的具体问题:

  1. 是的,GPX 是一种非常好的共享位置数据的格式。这就是它的用途。

  2. 我没有执行此操作的代码,但您需要遍历您的 CLLocations 数组并随时构建 xml 数据结构。使用支持写入的 xml 解析器之一。(请参阅下面的链接)。

  3. Objective C 中没有标准的 GPX 解析器,但 GPX 只是 xml,因此您可以使用任何在 iOS 或 OSX 下工作的 xml 解析器。Ray Wenderlich 有一个关于在 iOS 下使用 xml 的非常好的教程,它解释了如何根据您的需要选择正确的解析器。这不是 GPX 特有的,但 GPX 只是 xml 的一种。原理是一样的。

于 2012-01-06T09:18:37.683 回答
3

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 交换格式)文件以.gpx扩展名结尾

您所要做的就是迭代 CLLocations 数组并创建标签(wpx)并将文件保存为 .gpx

我希望这有帮助

于 2013-11-17T11:46:38.023 回答
-2

使用 Xcode 5,添加新文件 -> 资源 -> GPX 文件。

于 2014-03-11T00:21:11.100 回答