gpt4 book ai didi

ios - Swift - 如何从 gpx 文件中读取坐标

转载 作者:可可西里 更新时间:2023-10-31 23:55:36 25 4
gpt4 key购买 nike

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

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

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

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

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

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

提前致谢-豪尔赫

最佳答案

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

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)!))
}
}
}

希望对大家有帮助

关于ios - Swift - 如何从 gpx 文件中读取坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38507289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com