Swift地理编码和反地理编码

2020-05-03  本文已影响0人  越天高

使用

location : CLLocation 类型, 位置对象信息, 里面包含经纬度, 海拔等等
region: CLRegion 类型, 地标对象对应的区域
addressDictionary  : NSDictionary 类型, 存放街道,省市等信息
name: NSString 类型, 地址全称
thoroughfare: NSString 类型, 街道名称
locality: NSString 类型, 城市名称
administrativeArea : NSString 类型, 省名称
country: NSString 类型, 国家名称

geocode.geocodeAddressString("南京")
         { (marks:[CLPlacemark]?, error) in
           if error == nil
           {
               print("地理编码成功")
               guard let allMark = marks else {return}
               let mark = allMark.first
               self.latitudeTF.text = "\(mark!.location!.coordinate.latitude)"
               self.longitudeTF.text = "\(mark!.location!.coordinate.longitude)"
               self.addressTextView.text = mark?.name
               
           }
            
        }
 @IBAction func reverseGeoCodeBtnClick(_ sender: Any)
    {
        let latitudeText = CLLocationDegrees(self.latitudeTF.text!) ?? 0
        let longitudeText = CLLocationDegrees(self.longitudeTF.text!) ?? 0
        
        
        
        geocode.reverseGeocodeLocation(CLLocation(latitude: latitudeText, longitude: longitudeText))
              
       { (marks : [CLPlacemark]?, error) in
          if error == nil
          {
              print("反地理编码成功")
            guard let allMark = marks else {return}
            
            for place in allMark
            {
                print(place.name)
            }
            let firstPlace = allMark.first            
            self.addressTextView.text = firstPlace?.name
          }else
          {
            print("反f地理编码错误")
          }
        }
    }
  1. 定位
  2. 定位后, 根据位置信息, 进行反地理编码
    代码
 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        print("开始定位")
        guard let newLocation = locations.last else
        {
            return
        }
        
        if newLocation.horizontalAccuracy < 0 {
            return
        }
        
        geocode.reverseGeocodeLocation(CLLocation(latitude: newLocation.coordinate.latitude, longitude: newLocation.coordinate.longitude))
               
        { (marks : [CLPlacemark]?, error) in
           if error == nil
           {
               print("反地理编码成功")
             guard let allMark = marks else {return}
             
             for place in allMark
             {
                 print(place.name)
             }
             let firstPlace = allMark.first
             
             self.addressTextView.text = firstPlace?.locality
             
           }else
           {
             print("反f地理编码错误")
         }
         
         }
        
上一篇 下一篇

猜你喜欢

热点阅读