【问题标题】:Creating GPX file from array of CLLocation从 CLLocation 数组创建 GPX 文件
【发布时间】:2012-02-01 10:10:03
【问题描述】:

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

  1. 不能说
  2. 我看到一些网页将点的数据转换为 GPX 格式 但找不到他们是如何做到的。
  3. 没有

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

【问题讨论】:

  • 我不相信您的应用程序会读取 GPX 文件,更多的是由模拟器本身将位置信息输入应用程序。 Xcode 中有一个位置箭头,您可以单击以加载您的 GPX 文件。还有一些默认位置,您也可以查看。

标签: iphone ios parsing core-location gpx


【解决方案1】:

回答您的具体问题:

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

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

  3. Objective C 中没有标准的 GPX 解析器,但 GPX 只是 xml,因此您可以使用任何在 iOS 或 OSX 下工作的 xml 解析器。 Ray Wenderlich 有a very good tutorial on on using xml under iOS,它解释了如何根据您的需要选择正确的解析器。这不是 GPX 特有的,但 GPX 只是 xml 的一种。原理是一样的。

【讨论】:

  • 非常感谢。那很有帮助。另一个小问题,你用过 GeoJSON 吗?它的性能与 GPX 相比如何?
  • 性能完全是另一个问题。我会坚持使用普通的 GPX,除非你的文件太大以至于性能有问题。另请注意,GPX 支持 GPS 概念,如轨迹、路线和航点,但 GeoJSON 更像 GIS,并使用点、线串、多边形等。您可以使用任何一种格式,但如果您表示路线,则 GPX 可能更自然适合您的应用
  • 更新:现在有一个用于在 iOS 下解析 GPX 的开源库,iOS GPX Framework。源代码在 Watanabe Toshinori's github site 上,还有一个 KML 库和展示如何使用这些库的示例应用程序。
  • 如何在 GPX 文件中写入运行时用户位置 lat long 我的目标是我想与另一个用户共享用户旅行路线
【解决方案2】:

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

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

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

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

【讨论】:

【解决方案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

希望对你有帮助

【讨论】:

  • 如何在 GPX 文件中写入运行时用户位置 lat long
  • 有很多第三方库可以做到这一点,你也可以通过字符串构建的方式自己做。
【解决方案4】:

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

【讨论】:

  • OP 询问的是如何从内存中的 CLLocation 点列表创建自定义 GPX 文件,而不是如何在 Xcode 中手动创建 GPX 文件。
【解决方案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 = ""
  }
}

【讨论】:

    猜你喜欢
    • 2012-08-18
    • 2021-02-16
    • 2015-05-28
    • 2014-05-24
    • 1970-01-01
    • 2011-07-09
    • 2018-12-28
    • 2017-01-04
    • 2013-03-09
    相关资源
    最近更新 更多