地图1
2022-10-09 本文已影响0人
Gabriella0629
791499-20161203154717037-1773789467.png
extension MainVC: AMapSearchDelegate, CLLocationManagerDelegate{
func setuploacalManager() {
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
// 定位成功
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
let req = LngLatToCityRequest()
req.longitude = location.coordinate.longitude.string
req.latitude = location.coordinate.latitude.string
///城市编码
// 反地理编码转换成具体的地址
gecoder.reverseGeocodeLocation(location) { placeMarks, _ in
// gecoder.citycode
// CLPlacemark -- 国家 城市 街道
if let placeMark = placeMarks?.first {
print(placeMark.region as Any)
print("\(placeMark.country!) -- \(placeMark.name!) -- \(placeMark.locality!)")
let barButtonItem = self.navigationItem.rightBarButtonItems![1]
let loacalBtn = barButtonItem.customView as! UIButton
loacalBtn.setTitle(placeMark.locality!, for: .normal)
}
}
}
// 停止位置更新
locationManager.stopUpdatingLocation()
}
// 定位失败
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
}