swift 高德地图之定位获取地址信息

高德地图中,在func mapView(_ mapView:MAMapView!, didUpdate userLocation: MAUserLocation!, updatingLocation:Bool ) 中取其定位信息,获取MAUserLocation,通过geocoder.reverseGeocodeLocation(location!) 获取当前位置的地址信息,国家 省  市  区  街道  名称  国家编码  邮编等都可以获取。

//CLLocation
let location = userLocation.location​​​​​​​

将获取的MAUserLocation对象传进方法里即可。

    var clousre : MKPositioningClosure?
    func getAddress(userLocation: MAUserLocation) {
        //CLLocation
        let location = userLocation.location
        let geocoder = CLGeocoder()
        
        let latitude = location?.coordinate.latitude
        let longitude = location?.coordinate.longitude
        
        geocoder.reverseGeocodeLocation(location!) { (placemarks, error) in
            if error != nil {
                if self.clousre != nil {
                    self.clousre!(error?.localizedDescription ?? "")
                }
                return
            }
            
            if let place = placemarks?[0]{
                // 国家 省  市  区  街道  名称  国家编码  邮编
                //                let country = place.country ?? ""
                let name = place.name ?? ""
                let thoroughfare = place.thoroughfare ?? ""
                let subThoroughfare = place.subThoroughfare ?? ""
                let administrativeArea = place.administrativeArea ?? ""
                let locality = place.locality ?? ""
                let subLocality = place.subLocality ?? ""
                let subAdministrativeArea = place.subAdministrativeArea ?? ""
                let postalCode = place.postalCode ?? ""
                let isoCountryCode = place.isoCountryCode ?? ""
                let country = place.country ?? ""
                let inlandWater = place.inlandWater ?? ""
                let ocean = place.ocean ?? ""
                let areasOfInterest = place.areasOfInterest ?? [""]
                
                let addressLines =  administrativeArea + locality + subLocality + thoroughfare + name
                print("****************************")
                let ss = "\(subThoroughfare),\(subAdministrativeArea),\(postalCode),\(isoCountryCode),\(country),\(inlandWater),\(ocean),\(areasOfInterest)"
                print(addressLines)
                print(ss)
                self.clousre!(addressLines)
            } else {
                
                if self.clousre != nil {
                    self.clousre!("No placemarks!")
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_31622345/article/details/101058880