地理编码地址不返回正确的坐标
问题描述:
我想从地址字符串获取坐标并将地图标记放置在我的地图视图中,但我没有从地理编码器获取正确的坐标。我看到有几个问题。地理编码地址不返回正确的坐标
一,地图上的注释不是它们应该在的地方,因此不能从我的地理编码器获得正确的坐标。他们应该都相当接近,但他们不是。
二,应该有三个注释,但只有两个出现。难道我做错了什么?
我的完整方法。我的数据库目前有经度和纬度,但我试图避免在那里,如果我能得到这个工作。
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)
答
我通过将for循环中的所有代码移动到地理编码完成处理程序来解决此问题
我的猜测是:'geocodeAddressString()'是异步的。所以举个例子,如果你写了一个日志,例如当你将注释附加到'gymLocations'上时记录,而当你showAnnations()时你会看到。 – Larme
@Larme看到我的问题的更新。 – raginggoat
问题是:您看到日志出现的顺序是什么?我仍然认为你的问题是关于异步的。 – Larme