[IOS开发记录]ios10下使用Xcode8.2获取gps位置信息(swift3.0)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weizhixiang/article/details/54178357

CoreLocation是iOS中一个提供设备定位的框架。通过这个框架可以实现定位处理,从而获取位置数据,比如经度、纬度、海拔信息等。


默认环境为Xcode8.2和swift3.0


第一步:先在info.plist中设置位置权限 

有三种权限

Privacy - Location Usage Description  //这个不太清楚

Privacy - Location Always Usage Description   //始终使用的提示信息

Privacy - Location When In Use Usage Description   //使用期间使用位置信息

这三种权限和后面 locationManager.request的方式相对应的也可以都添加,如果只在使用期间使用可以只添加Privacy - Location When In Use Usage Description后面写上说明,显得程序过度自然

 

 


第二步几个关键设置:


1.定位精度的设置

定位服务管理类CLLocationManager的desiredAccuracy属性表示精准度,有如下6种选择:

kCLLocationAccuracyBestForNavigation :精度最高,一般用于导航

kCLLocationAccuracyBest : 精确度最佳

kCLLocationAccuracyNearestTenMeters :精确度10m以内

kCLLocationAccuracyHundredMeters :精确度100m以内

kCLLocationAccuracyKilometer :精确度1000m以内

kCLLocationAccuracyThreeKilometers :精确度3000m以内


2.位置管理器更新频率的设置

我们无法直接控制位置管理器更新的频率,但可使用位置管理器的distanceFilter属性(单位米)进行间接控制。

locationManager.distanceFilter = 100//单位为m

它指设备(水平或垂直)移动多少米后才将另一个更新发送给委托。一般来说定位要求的精度越高,distanceFilter属性的值越小,应用程序的耗电量就越大。


3.计算两个坐标间的距离

通过CCLocation对象的distanceTo方法,可以得到两个坐标间的距离,单位是米。

        let currentLocation = CLLocation(latitude: 52.104526, longitude: 51.111151)

        let targetLocation = CLLocation(latitude: 52.105526, longitude: 51.141151)

        let distance:CLLocationDistance = currentLocation.distance(from: targetLocation)

        print("两点间距离是:\(distance)")


第三步完整代码:

import UIKit
import CoreLocation

class ViewController: UIViewController,CLLocationManagerDelegate {
    let locationManager:CLLocationManager = CLLocationManager()
    let newLabel=UILabel(frame: CGRect(x: 100, y: 200, width: 300, height: 100))

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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

第四步:如果用户拒绝定位权限,下次进入程序跳转至系统设置

override func viewDidAppear(_ animated: Bool) {
        
        if(CLLocationManager.authorizationStatus() != .denied) {
            print("应用拥有定位权限")
        }else {
            let aleat = UIAlertController(title: "打开定位开关", message:"定位服务未开启,请进入系统设置>隐私>定位服务中打开开关,并允许xxx使用定位服务", preferredStyle: .alert)
            let tempAction = UIAlertAction(title: "取消", style: .cancel) { (action) in
            }
            let callAction = UIAlertAction(title: "立即设置", style: .default) { (action) in
                let url = NSURL.init(string: UIApplicationOpenSettingsURLString)
                if(UIApplication.shared.canOpenURL(url! as URL)) {
                    //ios10废弃openurl
                    UIApplication.shared.open(url! as URL, options: [:], completionHandler: nil)
                    
                }
            }
            aleat.addAction(tempAction)
            aleat.addAction(callAction)
            self.present(aleat, animated: true, completion: nil)
        }
        
    }

参考:http://www.hangge.com/blog/cache/detail_783.html


猜你喜欢

转载自blog.csdn.net/weizhixiang/article/details/54178357