Meine Eingabe ist ein Breiten- und Längengrad. Ich muss die Funktion reverseGeocodeLocation
von Swift verwenden, um die Ausgabe des Ortes zu erhalten. Der Code, den ich versucht habe, ist
println(geopoint.longitude)
println(geopoint.latitude)
var manager : CLLocationManager!
var longitude :CLLocationDegrees = geopoint.longitude
var latitude :CLLocationDegrees = geopoint.latitude
var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
println(location)
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error) -> Void in
println(manager.location)
if error != nil {
println("Reverse geocoder failed with error" + error.localizedDescription)
return
}
if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
println(pm.locality)
}
else {
println("Problem with the data received from geocoder")
}
in den Protokollen bekomme ich
//-122.0312186
//37.33233141
//C.CLLocationCoordinate2D
//fatal error: unexpectedly found nil while unwrapping an Optional value
Es scheint, dass die CLLocationCoordinate2DMake
Funktion schlägt fehl, was dann den schwerwiegenden Fehler in der reverseGeocodeLocation
-Funktion verursacht. Habe ich das Format irgendwo durcheinander gebracht?
sie kehren die Position nie um, sondern übergeben sie in manager.location.
siehe: CLGeocoder().reverseGeocodeLocation(manager.location, ...
Ich nehme an, das war ein Copy & Paste-Fehler und das ist das Problem - der Code selbst sieht gut aus - fast;)
var longitude :CLLocationDegrees = -122.0312186
var latitude :CLLocationDegrees = 37.33233141
var location = CLLocation(latitude: latitude, longitude: longitude) //changed!!!
println(location)
CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
println(location)
if error != nil {
println("Reverse geocoder failed with error" + error.localizedDescription)
return
}
if placemarks.count > 0 {
let pm = placemarks[0] as! CLPlacemark
println(pm.locality)
}
else {
println("Problem with the data received from geocoder")
}
})