地图和定位(一)

2020-03-07  本文已影响0人  weyan

地理编码: 广州市天河区xx街盛达商务区 -------> 113.23456 , 23.498897
反地理编码:113.23456 , 23.498897 --------> 广州市天河区xx街盛达商务区

一、定位

1、iOS8.0之前的定位

用户隐私 开启后台定位
-------------------------------ViewController.swift---------------------------------
import UIKit
import CoreLocation

class ViewController: UIViewController {
    var locationManager: CLLocationManager?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //开始获取用户的位置信息
        
        //1.创建位置管理者
        locationManager = CLLocationManager()
        //1.1设置代理
        locationManager!.delegate = self
        //2.使用位置管理者,开始获取用户的位置信息
        //小经验:如果想要使用位置管理者实现某个服务,可以以start开始某个服务,stop结束某个服务
        //ing意味着,一旦调用了这个方法,就会不断的获取用户的位置信息,然后告诉外界
        locationManager!.startUpdatingLocation()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension ViewController:CLLocationManagerDelegate {
    /**更新到位置信息时调用
     * manager: 位置管理者
     * location: 位置数组
     */
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("定位到了")
    }
}

2、 iOS8.0之后的定位

(1).iOS8.0之后的前台定位

-----------------------------ViewController.swift----------------------------------
import UIKit
import CoreLocation

class ViewController: UIViewController {
    //懒加载创建一个位置管理者
    lazy var locationManager: CLLocationManager = {
        let locationM = CLLocationManager()
        locationM.delegate = self
        //请求授权
        //请求前台定位授权
        //使用这个方法,一定要注意需要在info.plist文件里面配置对应的key
        if #available(iOS 8.0, *){
            locationM.requestWhenInUseAuthorization()
        }
        return locationM
    }()
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //开始获取用户位置信息
        locationManager.startUpdatingLocation()
    }

}

extension ViewController: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("已经获取到了位置信息")
    }
}

2、iOS8.0之后的后台定位
方案一:

方案二:

3、iOS9.0之后的定位

-------------------------ViewController.swift------------------------
import UIKit
import CoreLocation

class ViewController: UIViewController {
    //懒加载创建一个位置管理者
    lazy var locationManager: CLLocationManager = {
        let locationM = CLLocationManager()
        locationM.delegate = self
        //在ios8.0之后,需要主动请求授权
        //一定不要忘记在info.plist文件中配置key
        if #available(iOS 8.0, *){
            locationM.requestWhenInUseAuthorization()
            if #available(iOS 9.0, *){
                //默认情况下只能在前台获取用户位置信息,如果想要在后台获取需要勾选后台模式location updates,但是如果当前的系统版本是iOS9.0+,那么需要额外的执行以下方法
                //如果设置这样的属性为true,那么必须勾选后台模式,否则会崩溃。(和iOS8.0的后台定位区别就在于下面这句代码)
                locationM.allowsBackgroundLocationUpdates = true
            }
        }
        
        return locationM
    }()
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //开始获取用户位置信息
        self.locationManager.startUpdatingLocation()
    }

}

extension ViewController: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("已经获取到了位置信息")
    }
}

4、监听用户授权状态

iOS8.0之前定位提醒用户开启定位跳转到设置界面需要设置 URL Types
------------------ViewController.swift------------------
import UIKit
import CoreLocation

class ViewController: UIViewController {
    //懒加载创建一个位置管理者
    lazy var locationManager: CLLocationManager = {
        let locationM = CLLocationManager()
        locationM.delegate = self
        //在ios8.0之后,需要主动请求授权
        //一定不要忘记在info.plist文件中配置key
        if #available(iOS 8.0, *){
            locationM.requestWhenInUseAuthorization()
//            locationM.requestAlwaysAuthorization()
            if #available(iOS 9.0, *){
                //默认情况下只能在前台获取用户位置信息,如果想要在后台获取需要勾选后台模式location updates,但是如果当前的系统版本是iOS9.0+,那么需要额外的执行以下方法
                //如果设置这样的属性为true,那么必须勾选后台模式,否则会崩溃。(和iOS8.0的后台定位区别就在于下面这句代码)
                locationM.allowsBackgroundLocationUpdates = true
            }
        }
        
        return locationM
    }()
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //开始获取用户位置信息
        self.locationManager.startUpdatingLocation()
    }

}

extension ViewController: CLLocationManagerDelegate {
    //获取用户位置信息
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("已经获取到了位置信息")
    }
    /**如果当前的授权状态发生变化时调用
   * manager:位置管理者
   * status: 授权状态
   */
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case CLAuthorizationStatus.notDetermined:
            print("用户没有决定")
        case CLAuthorizationStatus.restricted:
            print("受限制")
        case CLAuthorizationStatus.authorizedAlways:
            print("前后台定位授权")
        case CLAuthorizationStatus.authorizedWhenInUse:
            print("前台定位授权")
        case CLAuthorizationStatus.denied:
            if CLLocationManager.locationServicesEnabled(){
                print("真正的被拒绝")
                //如果以后再开发过程中,发现已经被用户拒绝了我们应该提醒用户
                //这边只写跳转的关键代码
                
                //注意:这个跳转只能在iOS8.0之后才能跳转到设置界面
                if #available(iOS 8.0, *){
                    let url: NSURL? = NSURL(string: UIApplicationOpenSettingsURLString)
                    if UIApplication.shared.canOpenURL(url! as URL){
                        UIApplication.shared.openURL(url! as URL)
                    }
                }
                
                //如果想要在iOS8.0之前跳转到设置界面方法
                //1.可以一步一步的截图告诉用户该怎么操作
                //2.prefs:root=LOCATION_SERVICES
//                guard let url: NSURL = NSURL(string: "prefs:root=LOCATION_SERVICES") else {
//                    return
//                }
//                if UIApplication.shared.canOpenURL(url as URL){
//                    UIApplication.shared.openURL(url as URL)
//                }
            }else {
                print("请打开定位服务")
            }
            
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读