2
我想從地址字符串獲取座標並將地圖標記放置在我的地圖視圖中,但我沒有從地理編碼器獲取正確的座標。我看到有幾個問題。地理編碼地址不返回正確的座標
一,地圖上的註釋不是它們應該在的地方,因此不能從我的地理編碼器獲得正確的座標。他們應該都相當接近,但他們不是。
二,應該有三個註釋,但只有兩個出現。難道我做錯了什麼?
我的完整方法。我的數據庫目前有經度和緯度,但我試圖避免在那裏,如果我能得到這個工作。
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)")
}
}
}
我打印語句的結果:
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)
添加打印語句打印每個項目的座標gymLocations:
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)
而且添加print語句正確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)
我的猜測是:'geocodeAddressString()'是異步的。所以舉個例子,如果你寫了一個日誌,例如當你將註釋附加到'gymLocations'上時記錄,而當你showAnnations()時你會看到。 – Larme
@Larme看到我的問題的更新。 – raginggoat
問題是:您看到日誌出現的順序是什麼?我仍然認爲你的問題是關於異步的。 – Larme