Ich versuche Koordinaten aus Adresszeichenfolgen zu erhalten und Markierungen in meiner Kartenansicht zu platzieren, aber ich bekomme nicht die richtigen Koordinaten vom Geocodierer. Es gibt ein paar Probleme, die ich sehe.Geokodierungsadresse wird nicht zurückgegeben Korrekte Koordinaten
Eines, die Annotation auf der Karte sind nicht wo sie sein sollten, daher nicht die richtigen Koordinaten aus meinem Geocoder. Sie sollten alle ziemlich nah beieinander sein, aber sie sind es nicht.
Zwei, es sollte drei Anmerkungen geben, aber nur zwei werden angezeigt. Mache ich etwas falsch?
Meine vollständige Methode. Meine Datenbank hat zur Zeit Breiten- und Längengrad, aber ich versuche, das dort zu vermeiden, wenn ich das funktioniere.
func checkDistanceAndAddPins() {
for gym in gyms {
var index = 0
let gymLatitude = gym["latitude"]!!.doubleValue
let gymLongitude = gym["longitude"]!!.doubleValue
let gymLocation = CLLocation(latitude: gymLatitude, longitude: gymLongitude)
let distance = gymLocation.distanceFromLocation(myLocation!)
let distanceInMeters = NSNumber(double: distance)
let metersDouble = distanceInMeters.doubleValue
let miles = metersDouble * 0.00062137
if miles > maxDistance {
gyms.removeAtIndex(index)
} else {
// let location = CLLocationCoordinate2D(latitude: gymLatitude, longitude: gymLongitude)
gymAnnotation = GymAnnotation()
gymAnnotation!.title = gym["name"] as? String
gymAnnotation!.subtitle = gym["address"] as? String
let addressString = gym["address"] as? String
print("Address: \(addressString)")
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(addressString!, completionHandler: {
(placemarks, error) -> Void in
if error != nil {
print("Error", error)
}
if let placemark = placemarks?.first {
print(placemark.location!.coordinate)
self.gymAnnotation!.coordinate = placemark.location!.coordinate
}
})
// gymAnnotation!.coordinate = location
gymAnnotation!.gymPhoneNumber = gym["phone"] as? String
gymAnnotation!.gymID = gym["id"] as? String
if let website = gym["website"] as? String {
gymAnnotation!.gymWebsite = website
}
gymLocations.append(gymAnnotation!)
}
index += 1
}
if self.gymLocations.count == 0 {
let messageString = String(format: "There are no gyms within %.0f miles of your current location. Try changing the search radius.", maxDistance)
let noGymsAlert = UIAlertController(title: "", message: messageString, preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Default, handler: {
(action) -> Void in
noGymsAlert.dismissViewControllerAnimated(true, completion: nil)
})
let radiusAction = UIAlertAction(title: "Change Radius", style: .Default, handler: {
(action) -> Void in
noGymsAlert.dismissViewControllerAnimated(true, completion: nil)
self.changeSearchDistance()
})
noGymsAlert.addAction(radiusAction)
noGymsAlert.addAction(okAction)
noGymsAlert.view.tintColor = BarItems.greenTintColor
self.presentViewController(noGymsAlert, animated: true, completion: nil)
}
for item in gymLocations {
print("gymLocations Coordinates: \(item.coordinate)")
}
dispatch_async(dispatch_get_main_queue()) {
self.gymMap.addAnnotations(self.gymLocations)
self.gymMap.showAnnotations(self.gymMap.annotations, animated: true)
for item in self.gymMap.annotations {
print("annotations: \(item.coordinate)")
}
}
}
Meine print-Anweisung Ergebnisse:
Address: Optional("1753 Bardstown Rd, Louisville, KY 40205")
Address: Optional("8609 Westport Rd, Louisville, KY 40205")
Address: Optional("Mid City Mall, 1250 Bardstown Rd, Louisville, KY 40204")
CLLocationCoordinate2D(latitude: 38.235615099999997, longitude: -85.716183599999994)
CLLocationCoordinate2D(latitude: 38.22886264358975, longitude: -85.70138458380427)
CLLocationCoordinate2D(latitude: 38.281753100000003, longitude: -85.593839399999993)
Added print-Anweisung Koordinaten für jedes Element in gymLocations drucken:
gymLocations Coordinates: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
gymLocations Coordinates: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
gymLocations Coordinates: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
Und hinzugefügt print-Anweisung nach rechts nach showAnnotations()
annotations: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
annotations: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
annotations: CLLocationCoordinate2D(latitude: 38.21228, longitude: -85.679669000000004)
annotations: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
Meine Vermutung: 'geocodeAddressString()' ist async. Wenn Sie zum Beispiel ein Protokoll anlegen, indem Sie beispielsweise die Annotation an 'gymLocations' anhängen und wenn Sie' annations() 'anzeigen, werden Sie sehen. – Larme
@Larme sehe das Update auf meine Frage. – raginggoat
Die Frage ist: In welcher Reihenfolge sehen Sie die Protokolle angezeigt? Ich denke immer noch, dass es sich um Async handelt. – Larme