iOS定位、獲取方向、區域監測

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,
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Getting the User’s Location ios官方文檔:https://developer.app...
    lhg_serven閱讀 4,369評論 0 2
  • 定位 定位功能 定位主要用于獲取用戶當前地理位置信息。 常用的定位技術:GPS定位(全球定位系統:最精準的定位方式...
    misakatao閱讀 5,817評論 2 9
  • 直接進入主題 在App開發中定位和地圖關系緊密,定位使用的類通常以CL開頭,而地圖服務的類以MK開頭),可以只定位...
    Smallwolf_JS閱讀 29,362評論 8 25
  • iOS中的定位功能 CoreLocation框架(CoreLocation.framework)可用于定位設備當前...
    請叫我張折騰閱讀 2,524評論 0 1
  • 簡介: 在移動互聯網時代,移動app能解決用戶的很多生活瑣事,比如周邊:找餐館、找KTV、找電影院等等 導航:根據...
    冰鏡止水閱讀 1,364評論 0 4