美文网首页
iOS定位、获取方向、区域监测

iOS定位、获取方向、区域监测

作者: 圣艾修 | 来源:发表于2023-09-07 09:17 被阅读0次

    iOS设备能提供3种不同途径进行定位:Wifi, 蜂窝式移动电话基站, GPS卫星,它是由Core Location框架系统自动分配决定,最终都采用CLLocationManager类来实现定位。

    CoreLocation框架 (CoreLocation.framework)可用于定位设备当前经纬度, 通过该框架, 应用程序可计算出用户位置.

    其中CLLocationManager是整个CoreLocation框架的核心,定位、方向检测、区域检测等都由该API完成,CLLocationManagerDelegate是一个协议,实现该协议的对象可作为CLLocationManager的delegate对象,负责处理CLLocationManager的定位、方向检测、区域检测等相关事件。

    本篇文章提供定位的三个用途

    • 获取位置
    • 获取方向
    • 区域监测

    1.授权应用

    首先在 info.plis 添加允许在前台获取GPS的描述和允许在前、后台获取GPS的描述

    NSLocationWhenInUseUsageDescription
    NSLocationAlwaysUsageDescription
    
    

    2.引用框架

    import CoreLocation
    

    3.声明变量

    /// 声明一个全局变量
    var locationManager:CLLocationManager!
    

    4.实现调用方法

    func loacation() {
        locationManager = CLLocationManager()
        /// 设置定位的精确度
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        /// 设置定位变化的最小距离 距离过滤器
        locationManager.distanceFilter = 50
        /// 设置请求定位的状态
        if #available(iOS 8.0, *) {
            /// 用户使用期间
            locationManager.requestWhenInUseAuthorization()
            /// 总是允许
            locationManager.requestAlwaysAuthorization()
        }
        /// 设置代理
        locationManager.delegate = self;
        if CLLocationManager.locationServicesEnabled(){
            /// 开启定位服务
            locationManager.startUpdatingLocation()
            /// 开启方向服务
            locationManager.startUpdatingHeading()
            /// 先指定位置中心
            var companyCenter = CLLocationCoordinate2D()
            companyCenter.latitude = 29.61710109843841
            companyCenter.longitude = 106.49967413405561
            /// 设置一个方圆200米的范围
            let regin = CLCircularRegion.init(center: companyCenter, radius: 200, identifier: "id")
            /// 开始监听有没有进入、离开这个区域
            locationManager.startMonitoring(for: regin)
        } else {
            print("没有定位服务")
        }
    }
    

    5.实现代理方法

    extension ViewController: CLLocationManagerDelegate {
        /// 定位失败调用的代理方法
        func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
            print(error.localizedDescription)
        }
        
        /// 当定位授权状态改变
        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            switch status {
                case .notDetermined:
                    print("未决定")
                case .restricted:
                    print("未授权")
                case .denied:
                    print("拒绝")
                case .authorizedAlways:
                    print("总是允许")
                case .authorizedWhenInUse:
                    print("仅使用时")
                @unknown default:
                    print("")
            }
        }
    
        /// 定位更新地理信息调用的代理方法
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            /// 停止定位
            manager.stopUpdatingLocation()
            /// 获取用户位置的对象
            if locations.count > 0, let info = locations.last {
                print("海拔:\(info.altitude),经度:\(info.coordinate.longitude), 纬度:\(info.coordinate.latitude)")
                let coder = CLGeocoder()
                coder.reverseGeocodeLocation(info) { marks, error in
                    if let list = marks, list.count > 0{
                        let placemark = list[0]
                        print("国家:\(placemark.country!) 城市:\(placemark.locality!) 区县:\(placemark.subLocality!) 街道:\(placemark.thoroughfare!)")
                    }
                }
                
            }
    
         }
         
        /// 获取设备的朝向
        func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
            print("X:\(newHeading.x),Y:\(newHeading.y),Z:\(newHeading.z),")
            
        }
        
            /// 进入指定区域
        func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
            let alertController = UIAlertController(title: "提示", message: "进入考核区域", preferredStyle: .alert)
            let action = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(action)
            self.present(alertController, animated: true, completion: nil)
        }
        
        /// 离开指定区域
        func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
            let alertController = UIAlertController(title: "提示", message: "离开考核区域", preferredStyle: .alert)
            let action = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(action)
            self.present(alertController, animated: true, completion: nil)
        }
    }
    

    6.日志查看

    海拔:296.6501636505127,经度:106.49976393700089, 纬度:29.616418640486554
    国家:中国 城市:重庆市 区县:渝北区 街道:星光二路
    
    X:-25.39063835144043,Y:1.7516708374023438,Z:-19.735870361328125,
    X:-24.315837860107422,Y:2.12103271484375,Z:-20.7313232421875,
    

    相关文章

      网友评论

          本文标题:iOS定位、获取方向、区域监测

          本文链接:https://www.haomeiwen.com/subject/ftilvdtx.html