Ios Swift-如何从gpx文件读取坐标
在我的问题中,我发现我可以轻松创建gpx文件,但现在我需要将gpx文件的内容显示为一个多边形。以前,我在plist文件中创建了包含所有坐标的列表,这很容易阅读,因为我可以制作一个NSDictionary并从那里读取,然后使用plist提供的键查找位置,但是在gpx文件中它似乎不那么容易 我创建了这段代码片段来读取gpx文件的全部内容:Ios Swift-如何从gpx文件读取坐标,ios,swift,xcode,mkmapview,gpx,Ios,Swift,Xcode,Mkmapview,Gpx,在我的问题中,我发现我可以轻松创建gpx文件,但现在我需要将gpx文件的内容显示为一个多边形。以前,我在plist文件中创建了包含所有坐标的列表,这很容易阅读,因为我可以制作一个NSDictionary并从那里读取,然后使用plist提供的键查找位置,但是在gpx文件中它似乎不那么容易 我创建了这段代码片段来读取gpx文件的全部内容: if fileManager.fileExistsAtPath(filePath) { let dataBuffer = NSData(c
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>
测试
长度:1.339公里(0.832英里)
我只需要
标记之间的纬度和经度,这样我就可以将它们转换为位置,并从那里将其转换为多边形
任何帮助都将不胜感激,因为我还没有在谷歌找到任何关于如何使用swift读取gpx文件的信息
提前谢谢
-Jorge好的,我可以用以下代码读取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)!))
}
}
}
<代码>导入基础
导入地图套件
//解析gpx文件需要NSXMLParserDelegate,NSXMLParserDelegate需要NSObject
类TrackDrawer:NSObject,NSXMLParserDelegate{
//将检查所有文件名,如果找到,并且是gpx文件,则将生成多边形
变量文件名:[String]!=[String]()
初始化(文件名:[字符串]){
self.fileNames=文件名
}
//由于解析器函数无法返回值,因此需要为全局变量
私有变量边界=[CLLocationCoordinate2D]()
//为文件名中的每个字符串创建一个多边形
func getPolygons()->[MKPolygon]{
//将返回的列表
变量polyList:[MKPolygon]=[MKPolygon]()
用于文件名中的文件名{
//重置列表,使其不包含上一个多边形中的点
边界=[CLLocationCoordinate2D]()
//将文件名转换为计算机可读的文件路径
让filePath=getFilePath(文件名)
如果filePath==nil{
打印(“文件\”(文件名).gpx\”在项目中不存在。请确保您导入了该文件并且没有任何拼写错误)
持续
}
//设置解析器并使用文件路径的数据对其进行初始化
让data=NSData(contentsOfFile:filePath!)
让parser=NSXMLParser(数据:data!)
parser.delegate=self
//解析数据,在这里文件将被读取
让success=parser.parse()
//如果解析失败,则记录错误
如果!成功{
打印(“未能分析以下文件:\(fileName).gpx”)
}
//使用解析过程中生成的点创建多边形
polyList.append(MKPolygon(坐标:&边界,计数:边界.count))
}
返回多段列表
}
func getFilePath(文件名:String)->String{
//生成计算机可读的路径
返回NSBundle.mainBundle().pathForResource(文件名,类型:“gpx”)
}
func解析器(解析器:NSXMLParser,didStartElement元素名称:String,namespaceURI:String?,qualifiedName qName:String?,attributes attributeDict:[String:String]){
//只检查有或标记的行。其他行没有坐标,因此我们不感兴趣
如果elementName==“trkpt”| | elementName==“wpt”{
//从文件创建世界地图坐标
让lat=attributeDict[“lat”]!
让lon=attributeDict[“lon”]!
边界。附加(CLLocationCoordinate2DMake(CLLocationDegrees(lat)!,CLLocationDegrees(lon)!)
}
}
}
我希望它能迅速帮助别人
import Foundation
import CoreLocation
class Parser {
private let coordinateParser = CoordinatesParser()
func parseCoordinates(fromGpxFile filePath: String) -> [CLLocationCoordinate2D]? {
guard let data = FileManager.default.contents(atPath: filePath) else { return nil }
coordinateParser.prepare()
let parser = XMLParser(data: data)
parser.delegate = coordinateParser
let success = parser.parse()
guard success else { return nil }
return coordinateParser.coordinates
}
}
class CoordinatesParser: NSObject, XMLParserDelegate {
private(set) var coordinates = [CLLocationCoordinate2D]()
func prepare() {
coordinates = [CLLocationCoordinate2D]()
}
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
guard elementName == "trkpt" || elementName == "wpt" else { return }
guard let latString = attributeDict["lat"], let lonString = attributeDict["lon"] else { return }
guard let lat = Double(latString), let lon = Double(lonString) else { return }
guard let latDegrees = CLLocationDegrees(exactly: lat), let lonDegrees = CLLocationDegrees(exactly: lon) else { return }
coordinates.append(CLLocationCoordinate2D(latitude: latDegrees, longitude: lonDegrees))
}
}