I looked at Stanford University tutorial lesson 14.
but if I want to open a file.gpx in folder project loaded, I can not see in MapView.
This is the code:
func dataBaseURL()->NSURL {
let fileManager = NSFileManager.defaultManager()
let fileName = "Location01.gpx"
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let bundleURL = NSBundle.mainBundle().URLForResource("Location01", withExtension: "gpx")
var finalDatabaseURL:NSURL?
if let documentDirectory:NSURL = urls.first {
finalDatabaseURL = documentDirectory.URLByAppendingPathComponent(fileName)
if (finalDatabaseURL?.checkResourceIsReachableAndReturnError(nil) != nil) {
return finalDatabaseURL!
} else {
do {
try fileManager.copyItemAtURL(bundleURL!, toURL: finalDatabaseURL!)
}
catch let error as NSError {
/
print("Couldn't create temp file from: \(bundleURL) at: \(finalDatabaseURL) error: \(error.localizedDescription).")
print("Error\nCode: \(error.code)\nDomain: \(error.domain)\nDescription: \(error.localizedDescription)\nReason: \(error.localizedFailureReason)\nUser Info: \(error.userInfo)\n")
}
/
}
}
return finalDatabaseURL!
}
override func viewDidLoad() {
super.viewDidLoad()
let center = NSNotificationCenter.defaultCenter()
let queue = NSOperationQueue.mainQueue()
let appDelegate = UIApplication.sharedApplication().delegate
center.addObserverForName(GPXURL.Notification, object: appDelegate, queue: queue) {
notification in if let url = notification.userInfo?[GPXURL.Key] as? NSURL {
self.gpxURL = url
}
}
gpxURL = dataBaseURL()
}
var gpxURL: NSURL? {
didSet {
clearWaypoints()
if let url = gpxURL {
GPX.parse(url) {
if let gpx = $0 { self.handleWaypoints(gpx.waypoints)}
}
}
}
}
private func clearWaypoints() {
if mapView?.annotations != nil {mapView.removeAnnotations(mapView.annotations as [MKAnnotation])}
}
private func handleWaypoints(waypoints:[GPX.Waypoint]) {
mapView.addAnnotations(waypoints)
mapView.showAnnotations(waypoints, animated: true)
}