返回 导航

Swift

hangge.com

Swift - 异步编程库PromiseKit使用详解11(CLGeocoder的扩展)

作者:hangge | 2018-12-26 08:10
    通过 CLGeocoder 类我们可以实现经纬度位置坐标与真实地理位置相互转化。PromiseKit 同样也对其进行了扩展,方便我们使用。

十二、CLGeocoder 的扩展

1,准备工作

首先我们同样要安装 PromiseKit 库,以及相关的 CoreLocation 扩展库,具体步骤参考我之前的文章:

2,使用样例

 (1)通过经纬度获取地址信息
import UIKit
import PromiseKit
import PMKCoreLocation

class ViewController: UIViewController {
    
    @IBOutlet weak var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //需要解析的经纬度坐标
        let currentLocation = CLLocation(latitude: 32.029171, longitude: 118.788231)
        //根据经纬度坐标获取地址信息
        CLGeocoder().reverseGeocode(location: currentLocation)
            .done { placemarks in
                //强制转成简体中文
                let array = NSArray(object: "zh-hans")
                UserDefaults.standard.set(array, forKey: "AppleLanguages")
                //显示所有信息
                let p = placemarks[0]
                print(p) //输出反编码信息
                var address = "--- 通过经纬度获取到如下地址 ---\n"
                
                if let country = p.country {
                    address.append("国家:\(country)\n")
                }
                if let administrativeArea = p.administrativeArea {
                    address.append("省份:\(administrativeArea)\n")
                }
                if let subAdministrativeArea = p.subAdministrativeArea {
                    address.append("其他行政区域信息(自治区等):\(subAdministrativeArea)\n")
                }
                if let locality = p.locality {
                    address.append("城市:\(locality)\n")
                }
                if let subLocality = p.subLocality {
                    address.append("区划:\(subLocality)\n")
                }
                if let thoroughfare = p.thoroughfare {
                    address.append("街道:\(thoroughfare)\n")
                }
                if let subThoroughfare = p.subThoroughfare {
                    address.append("门牌:\(subThoroughfare)\n")
                }
                if let name = p.name {
                    address.append("地名:\(name)\n")
                }
                if let isoCountryCode = p.isoCountryCode {
                    address.append("国家编码:\(isoCountryCode)\n")
                }
                if let postalCode = p.postalCode {
                    address.append("邮编:\(postalCode)\n")
                }
                if let areasOfInterest = p.areasOfInterest {
                    address.append("关联的或利益相关的地标:\(areasOfInterest)\n")
                }
                if let ocean = p.ocean {
                    address.append("海洋:\(ocean)\n")
                }
                if let inlandWater = p.inlandWater {
                    address.append("水源,湖泊:\(inlandWater)\n")
                }
                self.textView.text = address
            }.catch { error in
                self.textView.text = "错误:\(error.localizedDescription))"
        }
    }
}

(2)通过地址获取对应的经纬度
import UIKit
import PromiseKit
import PMKCoreLocation

class ViewController: UIViewController {
    
    @IBOutlet weak var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //根据地址获取对应的经纬度坐标
        CLGeocoder().geocode("南京市新街口大洋百货")
            .done { placemarks in
                let p = placemarks[0]
                self.textView.text = "经度:\(p.location!.coordinate.longitude)   "
                    + "纬度:\(p.location!.coordinate.latitude)"
            }.catch { error in
                self.textView.text = "错误:\(error.localizedDescription))"
            }
    }
}
评论

全部评论(0)

回到顶部