func getAddressFromLatLon() {
var locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()
var currentLocation = CLLocation()
if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorized){
currentLocation = locManager.location!
}
var center : CLLocationCoordinate2D = CLLocationCoordinate2D()
let lat: Double = Double(currentLocation.coordinate.latitude)
//21.228124
let lon: Double = Double(currentLocation.coordinate.longitude)
//72.833770
let ceo: CLGeocoder = CLGeocoder()
center.latitude = lat
center.longitude = lon
let geoCoder = CLGeocoder()
var placemark: AnyObject
var error: NSError
geoCoder.reverseGeocodeLocation(locManager.location!, completionHandler: { (placemark, error) -> Void in
if error != nil {
print("Error: \(error?.localizedDescription)")
return
}
if (placemark?.count)! > 0 {
let pm = (placemark?[0])! as CLPlacemark
//self.addressString = pm.locality! + pm.country!
let adress = pm.locality! + " " + pm.country!
print(adress)
} else {
print("Error with data")
}
})
}
对不起,对于这个非常基本的问题。我对swift很陌生,我试图将地理代码反转为经纬度。我试图从反向地理编码返回地址,但它似乎返回零。有问题的函数是getAddressFromLatLon(),我尝试添加一个返回类型,但它返回零。当我在函数本身中打印时,会打印正确的值,但由于某种原因,我无法使地址返回,因此我可以将其传递给其他类/函数。退货方式Nill
FUNC getAddressFromLatLon()方法是一个无效功能。你不能指望它返回任何价值。 – Developer
还有一件事我想确认,是打印(地址)显示所需的结果?.. – Developer
你在这个功能拉长吗? – KKRocks