我想使用Lat long来获取地址,但总是变为零。请帮帮我,如何管理这种类型的功能,以便获得地址。我想在打电话的时候得到地址,但它总是给我的。
import re, locale, datetime
# considering dateString is the string representation of Date from Text
dateString = 'August 30th, 2017'
dateValues = re.search(r'(\w+)[\s](\d+)[A-Za-z\s,]*(\d+)', dateString)
if dateValues:
cleanDate = dateValues.groups(0)[0]+' '+dateValues.groups(0)[1]+', '+dateValues.groups(0)[2]
locale.setlocale(locale.LC_ALL, 'en_US')
publishedDate = datetime.datetime.strptime(cleanDate, '%B %d, %Y').strftime('%m%d%Y')
print publishedDate
答案 0 :(得分:1)
在功能开始时,您将addressString
设置为""
,并在调用ceo.reverseGeocodeLocation
的关闭之前返回此值。
不是直接从此函数返回值,而是需要使用闭包参数。你的功能应该更像这样(我添加了一些改进):
func getAddressFromLatLon(pdblLatitude: String, pdblLongitude: String, completion: @escaping (String) -> Void) {
var addressString : String = ""
var center : CLLocationCoordinate2D = CLLocationCoordinate2D()
let lat: Double = Double("\(pdblLatitude)")!
//21.228124
let lon: Double = Double("\(pdblLongitude)")!
//72.833770
let ceo: CLGeocoder = CLGeocoder()
center.latitude = lat
center.longitude = lon
let loc: CLLocation = CLLocation(latitude:center.latitude, longitude: center.longitude)
ceo.reverseGeocodeLocation(loc, completionHandler: { (placemarks, error) in
guard error != nil else {
print("reverse geodcode fail: \(error!.localizedDescription)")
return
}
let pm = placemarks![0]
print("ADDRESS \(pm.country)---\(pm.locality)---\(pm.subLocality)---\(pm.thoroughfare)---\(pm.postalCode)---\(pm.subThoroughfare)")
if pm.subLocality != nil {
addressString = addressString + pm.subLocality! + ", "
}
if pm.thoroughfare != nil {
addressString = addressString + pm.thoroughfare! + ", "
}
if pm.locality != nil {
addressString = addressString + pm.locality! + ", "
}
if pm.country != nil {
addressString = addressString + pm.country! + ", "
}
if pm.postalCode != nil {
addressString = addressString + pm.postalCode! + " "
}
completion(addressString)
})
}
文档指出,在reverseGeocodeLocation
的完成区中,placemarks
将为非空(如果它们不是nil
),则不需要进行额外检查