Swift 3:reverseGeocodeLocation 不调用其完成处理程序
Swift 3: reverseGeocodeLocation does not call its completion handler
这是我的代码
if loc.latitude != 0.0 && loc.longitude != 0.0 {
let loca = CLLocation(latitude: loc.latitude, longitude: loc.longitude)
geoCoder.reverseGeocodeLocation(loca) { (placemarks, error) in // this is the last line that is being called
var placemark : CLPlacemark!
placemark = placemarks?[0]
city = (placemark.addressDictionary?["City"] as! String)
}
}
此代码片段在我的应用程序中执行正常,没有发生运行时错误。
但是,调用的最后一行是
geoCoder.reverseGeocodeLocation(loca){(placemarks, error)
我还仔细检查了 loca
不为零。
为什么没有调用完成处理程序?
在闭包中使用 completionHandler
。
检查以下示例:
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// Address dictionary
print(placeMark.addressDictionary, terminator: "")
// Location name
if let locationName = placeMark.addressDictionary!["Name"] as? NSString {
print(locationName, terminator: "")
}
// Street address
if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString {
print(street, terminator: "")
}
// City
if let city = placeMark.addressDictionary!["City"] as? NSString {
print(city, terminator: "")
}
// Zip code
if let zip = placeMark.addressDictionary!["ZIP"] as? NSString {
print(zip, terminator: "")
}
// Country
if let country = placeMark.addressDictionary!["Country"] as? NSString {
print(country, terminator: "")
}
})
我遇到了这个问题,似乎对答案有些困惑。这是使用 Swift 3 获取位置信息的扩展方法,仅供以后遇到此问题的读者使用。
此功能代码使用didUpdateLocations
函数和reverseGeocodeLocation()
将位置转换为人类可读的地址。它还将地图视图设置为当前用户位置。这当然是假设您已经设置了位置管理器对象。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Get first location item returned from locations array
let userLocation = locations[0]
// Convert location into object with human readable address components
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) in
// Check for errors
if error != nil {
print(error ?? "Unknown Error")
} else {
// Get the first placemark from the placemarks array.
// This is your address object
if let placemark = placemarks?[0] {
// Create an empty string for street address
var streetAddress = ""
// Check that values aren't nil, then add them to empty string
// "subThoroughfare" is building number, "thoroughfare" is street
if placemark.subThoroughfare != nil && placemark.thoroughfare != nil {
streetAddress = placemark.subThoroughfare! + " " + placemark.thoroughfare!
} else {
print("Unable to find street address")
}
// Same as above, but for city
var city = ""
// locality gives you the city name
if placemark.locality != nil {
city = placemark.locality!
} else {
print("Unable to find city")
}
// Do the same for state
var state = ""
// administrativeArea gives you the state
if placemark.administrativeArea != nil {
state = placemark.administrativeArea!
} else {
print("Unable to find state")
}
// And finally the postal code (zip code)
var zip = ""
if placemark.postalCode != nil {
zip = placemark.postalCode!
} else {
print("Unable to find zip")
}
print("\(streetAddress)\n\(city), \(state) \(zip)")
}
}
}
// Create a coordinate based on users location latitude and longitude
let coordinate = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude,
longitude: userLocation.coordinate.longitude)
// Set the span (zoom) of the map view. Smaller number zooms in closer
let span = MKCoordinateSpan(latitudeDelta: 0.001, longitudeDelta: 0.001)
// Set the region, using your coordinates & span objects
let region = MKCoordinateRegion(center: coordinate, span: span)
// Set your map object's region to the region you just defined
map.setRegion(region, animated: true)
}
这是我的代码
if loc.latitude != 0.0 && loc.longitude != 0.0 {
let loca = CLLocation(latitude: loc.latitude, longitude: loc.longitude)
geoCoder.reverseGeocodeLocation(loca) { (placemarks, error) in // this is the last line that is being called
var placemark : CLPlacemark!
placemark = placemarks?[0]
city = (placemark.addressDictionary?["City"] as! String)
}
}
此代码片段在我的应用程序中执行正常,没有发生运行时错误。
但是,调用的最后一行是
geoCoder.reverseGeocodeLocation(loca){(placemarks, error)
我还仔细检查了 loca
不为零。
为什么没有调用完成处理程序?
在闭包中使用 completionHandler
。
检查以下示例:
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// Address dictionary
print(placeMark.addressDictionary, terminator: "")
// Location name
if let locationName = placeMark.addressDictionary!["Name"] as? NSString {
print(locationName, terminator: "")
}
// Street address
if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString {
print(street, terminator: "")
}
// City
if let city = placeMark.addressDictionary!["City"] as? NSString {
print(city, terminator: "")
}
// Zip code
if let zip = placeMark.addressDictionary!["ZIP"] as? NSString {
print(zip, terminator: "")
}
// Country
if let country = placeMark.addressDictionary!["Country"] as? NSString {
print(country, terminator: "")
}
})
我遇到了这个问题,似乎对答案有些困惑。这是使用 Swift 3 获取位置信息的扩展方法,仅供以后遇到此问题的读者使用。
此功能代码使用didUpdateLocations
函数和reverseGeocodeLocation()
将位置转换为人类可读的地址。它还将地图视图设置为当前用户位置。这当然是假设您已经设置了位置管理器对象。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Get first location item returned from locations array
let userLocation = locations[0]
// Convert location into object with human readable address components
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) in
// Check for errors
if error != nil {
print(error ?? "Unknown Error")
} else {
// Get the first placemark from the placemarks array.
// This is your address object
if let placemark = placemarks?[0] {
// Create an empty string for street address
var streetAddress = ""
// Check that values aren't nil, then add them to empty string
// "subThoroughfare" is building number, "thoroughfare" is street
if placemark.subThoroughfare != nil && placemark.thoroughfare != nil {
streetAddress = placemark.subThoroughfare! + " " + placemark.thoroughfare!
} else {
print("Unable to find street address")
}
// Same as above, but for city
var city = ""
// locality gives you the city name
if placemark.locality != nil {
city = placemark.locality!
} else {
print("Unable to find city")
}
// Do the same for state
var state = ""
// administrativeArea gives you the state
if placemark.administrativeArea != nil {
state = placemark.administrativeArea!
} else {
print("Unable to find state")
}
// And finally the postal code (zip code)
var zip = ""
if placemark.postalCode != nil {
zip = placemark.postalCode!
} else {
print("Unable to find zip")
}
print("\(streetAddress)\n\(city), \(state) \(zip)")
}
}
}
// Create a coordinate based on users location latitude and longitude
let coordinate = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude,
longitude: userLocation.coordinate.longitude)
// Set the span (zoom) of the map view. Smaller number zooms in closer
let span = MKCoordinateSpan(latitudeDelta: 0.001, longitudeDelta: 0.001)
// Set the region, using your coordinates & span objects
let region = MKCoordinateRegion(center: coordinate, span: span)
// Set your map object's region to the region you just defined
map.setRegion(region, animated: true)
}