//地理位置编码
let locationManager = CLLocationManager()
//地理位置代码
locationManager.delegate = self //代理CLLocationManagerDelegate
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager.stopUpdatingLocation()
let changeLocation:NSArray = locations as NSArray
let currentLocation = changeLocation.lastObject as! CLLocation
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(currentLocation) { (placemarks, error) in
if((placemarks?.count)! > 0){
let placeMark = placemarks?.first
if let currentCity = placeMark?.locality {
print("=============\(currentCity)")
}
print(placeMark?.country ?? "")
print(placeMark?.thoroughfare ?? "")
print(placeMark?.name ?? "")
print(placeMark?.subLocality ?? "")
}else if (error == nil && placemarks?.count == 0){
print("没有地址返回");
}
else if ((error) != nil){
print("location error\(String(describing: error))");
}
}
}
老铁们 别忘了添加地理位置权限......