iOS定位及解析经纬度【Swift】

满雨石
2023-12-01

导入

import CoreLocation
import MapKit

实现CLLocationManagerDelegate代理

初始化位置信息

  //    MARK:初始化位置
    func initLocation()  {
   
        locationManager.delegate = self
        //设置定位模式
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        //更新距离
        locationManager.distanceFilter = 5
        //               发送授权申请
        locationManager.requestWhenInUseAuthorization()
        if (CLLocationManager.locationServicesEnabled())
        {
            //允许使用定位服务的话,开启定位服务更新
            locationManager.startUpdatingLocation()
            print("定位开始")
        }
    }

获取设备经纬度

//MARK:CLLocationManagerDelegate
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    //  var currLocation:CLLocation!初始化!!!
        //获取最新的坐标
        currLocation = locations.last!
        //        //获取经度
        print("精度:\(currLocation.coordinate.longitude)")
        //获取纬度
        print("纬度:\(currLocation.coordinate.latitude)")
        //        //获取海拔
        print("海拔:\(currLocation.altitude)")
        //        //获取水平精度
        print("水平精度:\(currLocation.horizontalAccuracy)")
        //        //获取垂直精度
        print("垂直精度:\(currLocation.verticalAccuracy)")
        //        //获取方向
        print("方向:\(currLocation.course)")
        //        //获取速度
        print("速度:\(currLocation.speed)")
        
        reverseGeocode()
    }

reverseGeocode() 方法为地址返编码

 //MARK: 地理信息反编码
    func reverseGeocode(){
        let geocoder = CLGeocoder()
        let currentLocation = CLLocation(latitude: currLocation.coordinate.latitude, longitude: currLocation.coordinate.longitude)
        geocoder.reverseGeocodeLocation(currentLocation, completionHandler: {
            (placemarks:[CLPlacemark]?, error:Error?) -> Void in
            //强制转成简体中文
            let array = NSArray(object: "zh-hans")
            UserDefaults.standard.set(array, forKey: "AppleLanguages")
            //            //显示所有信息
            if error != nil {
                //print("错误:\(error.localizedDescription))")
                print("错误:\(error!.localizedDescription))")
                return
            }
            
            if let p = placemarks?[0]{
                print(p) //输出反编码信息
                var address = ""
                
                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")
                }
                
                print("111----\(address)")
            } else {
                print("No placemarks!")
            }
        })
    }
 //    MARK:定位失败、重新授权定位
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("定位失败")
    }
 类似资料: