Swift - 如何从 gpx 文件中读取坐标 [英] Swift - How to read coordinates from a gpx file

查看:41
本文介绍了Swift - 如何从 gpx 文件中读取坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在我的其他 我问的问题,我发现我可以轻松创建 gpx 文件,但现在我需要将 gpx 文件的内容显示为 MKPolygon.之前,我已经在 plist 文件中创建了包含所有坐标的列表,这很容易阅读,因为我可以制作一个 NSDictionary 并从那里读取它并使用 plist 提供的键找到位置,但是它没有在 gpx 文件中似乎很容易工作.

so in my other question I asked, I found out that I can create easily gpx files, but now I need to display the content of the gpx file as a MKPolygon. Before, I had created the list with all the coordinates in a plist file, this was easy to read since I could just make a NSDictionary and read it from there and finding the location using the keys that a plist supplies, however it doesn't seem to work this easy in a gpx file.

我创建了一小段代码来读取 gpx 文件的全部内容:

I've created this little snipped of code to read the whole content of the gpx file:

if fileManager.fileExistsAtPath(filePath) {
            let dataBuffer = NSData(contentsOfFile: filePath)
            let dataString = NSString(data: dataBuffer!, encoding: NSUTF8StringEncoding)
            print (dataString)
        }

所以现在我将整个文本放在一个字符串中,但我不需要所有这些:

So now I have the whole text in a string but I don't need all 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>

我只需要 <trkpt> 标签之间的纬度和经度,这样我就可以将它们转换为位置,然后再将其转换为 MKPolygon.

I just need the latitudes and longitudes between the <trkpt> tags so I can convert them into locations and from there convert it into a MKPolygon.

任何帮助将不胜感激,因为我在谷歌中没有找到任何关于如何快速读取 gpx 文件的信息.

Any help would be greatly appreciated as I haven't found anything in google on how to read gpx files with swift.

提前致谢-乔治

推荐答案

好的,我能够使用以下代码读取 gpx 文件:

Ok I was able to read the gpx file with the following code:

import Foundation
import MapKit

//NSXMLParserDelegate needed for parsing the gpx files and NSObject is needed by NSXMLParserDelegate
class TrackDrawer: NSObject, NSXMLParserDelegate {
    //All filenames will be checked and if found and if it's a gpx file it will generate a polygon
    var fileNames: [String]! = [String]()

    init(fileNames: [String]) {
        self.fileNames = fileNames
    }

    //Needs to be a global variable due to the parser function which can't return a value
    private var boundaries = [CLLocationCoordinate2D]()

    //Create a polygon for each string there is in fileNames
    func getPolygons() -> [MKPolygon]? {
        //The list that will be returned
        var polyList: [MKPolygon] = [MKPolygon]()

        for fileName in fileNames! {
            //Reset the list so it won't have the points from the previous polygon
            boundaries = [CLLocationCoordinate2D]()

            //Convert the fileName to a computer readable filepath
            let filePath = getFilePath(fileName)

            if filePath == nil {
                print ("File \"\(fileName).gpx\" does not exist in the project. Please make sure you imported the file and dont have any spelling errors")
                continue
            }

            //Setup the parser and initialize it with the filepath's data
            let data = NSData(contentsOfFile: filePath!)
            let parser = NSXMLParser(data: data!)
            parser.delegate = self

            //Parse the data, here the file will be read
            let success = parser.parse()

            //Log an error if the parsing failed
            if !success {
                print ("Failed to parse the following file: \(fileName).gpx")
            }
            //Create the polygon with the points generated from the parsing process
            polyList.append(MKPolygon(coordinates: &boundaries, count: boundaries.count))

        }
        return polyList
    }

    func getFilePath(fileName: String) -> String? {
        //Generate a 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 for the lines that have a <trkpt> or <wpt> tag. The other lines don't have coordinates and thus don't interest us
        if elementName == "trkpt" || elementName == "wpt" {
            //Create a World map coordinate from the file
            let lat = attributeDict["lat"]!
            let lon = attributeDict["lon"]!

            boundaries.append(CLLocationCoordinate2DMake(CLLocationDegrees(lat)!, CLLocationDegrees(lon)!))
        }
    }
}

希望对大家有所帮助

这篇关于Swift - 如何从 gpx 文件中读取坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆