iOS 定位说明
程序员文章站
2024-02-13 23:04:58
...
一、精度模式
extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation
//最适合导航用的定位 iOS4.0以后新增(我们现在用的是这个)
extern const CLLocationAccuracy kCLLocationAccuracyBest;
//精度最高的定位
extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
//定位精度在10米以内
extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
//定位精度在100米以内
extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
//定位精度在1000米以内
extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
//定位精度在3000米以内
二、左上角蓝条
必要条件:
1.app 开启 allowsBackgroundLocationUpdates 后台刷新
2.app 请求后台刷新(使用允许定位)、但是用户只给了使用期间允许定位,就会出现蓝条,当重新给定始终允许刷新时,蓝条消失。
app监测到用户给的权限低于申请的权限就会在app进入后台时,
停止刷新定位、此时就会出现(高德地图)左上角蓝条持续一会儿,就消失的情况。
代码如下:
//
// LocationManager.swift
// LocationUpdates
//
// Created by Quinn Von on 2020/3/20.
// Copyright © 2020 Quinn Von. All rights reserved.
//
import Foundation
import CoreLocation
import UIKit
class LocationManager:NSObject {
//MARK: - Properties
static let shared = LocationManager()
private var locationManager = CLLocationManager()
private var observersList = [String:LocationUpdateHandler]()
typealias LocationUpdateHandler = ((_ manager: CLLocationManager, _ locations: [CLLocation])->Void)
//MARK:- Initializer
override init() {
super.init()
setupLocationManager()
getLocationAuthentication()
print("Location Manager Initalized")
}
func stopLocationManager(){
locationManager.stopUpdatingLocation()
}
//MARK: - Functions
func addLocationUpdateObserver(_ observer: NSObject, completionHandler: @escaping LocationUpdateHandler) {
observersList[observer.description] = completionHandler
}
func removeLocationUpdateObserver(_ observer: NSObject){
observersList.removeValue(forKey: observer.description)
}
private func setupLocationManager(){
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.startUpdatingLocation()
locationManager.distanceFilter = 1.0
}
if #available(iOS 9.0, *) {
// 如果APP处于后台,则会出现蓝条
locationManager.allowsBackgroundLocationUpdates = true
}
}
func getLocationAuthentication(){
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
break
case .denied:
locationManager.requestWhenInUseAuthorization()
break
case .authorizedAlways:
break
case .restricted:
locationManager.requestWhenInUseAuthorization()
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
break
default:
break
}
}
}
extension LocationManager:CLLocationManagerDelegate{
//MARK: - CLLocationManagerDelegates
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//MARK: - Locations are being fetched here and posted to observer
observersList.forEach({$0.value(manager, locations)})
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
}
}
class SceneDelegate:
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
LocationManager.shared.stopLocationManager()
}
三、实心定位
APP 定位时,为实心的
四、空心定位
APP 定位时,但未成功,例如,app打开后迅速放到后台,有可能会出现、多试几次