Swift - 如何从 gpx 文件中读取坐标
Swift - How to read coordinates from a gpx file
所以在我提出的 问题中,我发现我可以轻松创建 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。
任何帮助将不胜感激,因为我在 google 中没有找到任何关于如何使用 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)!))
}
}
}
希望对大家有所帮助
Swift 方式
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))
}
}
所以在我提出的
我创建了这段代码片段来读取 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。
任何帮助将不胜感激,因为我在 google 中没有找到任何关于如何使用 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)!))
}
}
}
希望对大家有所帮助
Swift 方式
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))
}
}