从 CLLocation 数组创建 GPX 文件

发布于 2024-12-24 20:02:13 字数 292 浏览 8 评论 0原文

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

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

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

My application needs to share array of CLLocations (Route) within devices using application.I have no experience of using GPX before this. Is GPX is best format to do it? How can I create GPX file from given such array of CLLocations? and is there standard GPX parser in Objective C?
From what I have searched on net and SO answer to these questions are respectively

  1. Can't 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

I will be happy if I get alternate answers/views. I understand that these are lot of questions. Any help or suggestion will be hugely appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抽个烟儿 2024-12-31 20:02:13

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

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

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

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

Watanabe Toshinori just became your new best friend (mine as well), code is here:

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

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

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

梦回旧景 2024-12-31 20:02:13

这是 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 = ""
  }
}

Here it is in swift 4 just in case anyone needs it with a UIAlert to enter the filename

    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 = ""
  }
}
岁月苍老的讽刺 2024-12-31 20:02:13

回答您的具体问题:

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

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

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

To answer your specific questions:

  1. Yes, GPX is a very good format for sharing location data. Thats what its for.

  2. I don't have code to do this but you will need to iterate over your array of CLLocations and construct the xml data structure as you go. Use one the xml parsers that supports writing. (See the link below).

  3. There isn't standard GPX parser in Objective C, but GPX is just xml so you can use any xml parser that work under iOS or OSX. Ray Wenderlich has a very good tutorial on on using xml under iOS which explains how to choose the right parser for your needs. This is not GPX-specific but again GPX is just a flavor of xml; the principles are the same.

调妓 2024-12-31 20:02:13

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 数组并创建标签(wpx)并将文件另存为 .gpx

我希望这会有所帮助

GPX Sample:

<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>

The gpx(GPS eXchange Format) file ends with .gpx extension

All you have to do is just iterate the array of CLLocations and create the tags(wpx) and save the file as .gpx

I hope this helps

温柔戏命师 2024-12-31 20:02:13

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

With Xcode 5 , Add New File - > Resource - > GPX File .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文