Swift4 获取本地坐标,获取本地位置信息
在项目infoplist文件添加键值对
NSLocationAlwaysUsageDeion
NSLocationWhenInUseUsageDescription
NSLocationAlwaysAndWhenInUseUsageDescription
第三条在ios10以后不加的话,将不会弹出获取权限alert
使用懒加载,初始化CLLocationManager实例
lazy var locationM: CLLocationManager = {
let locationM = CLLocationManager()
locationM.requestAlwaysAuthorization()
//设备使用电池供电时最高的精度
locationM.desiredAccuracy = kCLLocationAccuracyBest
//精确到1000米,距离过滤器,定义了设备移动后获得位置信息的最小距离
locationM.distanceFilter = kCLLocationAccuracyKilometer
locationM.delegate = self
// 请求授权
if #available(iOS 8.0, *) {
// 请求前后台定位授权
// 请求, 前台定位授权
// 1. 获取info.plist文件内容
guard let infoDic = Bundle.main.infoDictionary else { return locationM }
// 2. 获取前台定位授权的key 值
let whenInUse = infoDic["NSLocationWhenInUseUsageDescription"]
// 3. 获取前后台定位授权key 值
let always = infoDic["NSLocationAlwaysUsageDescription"]
// 4. 判断: 如果两个都有, 请求权限比较高的哪一个
// 如果只有某一个, 那就请求对应的授权
// 如果两个都没有, 给其他开发者提醒
if always != nil {
locationM.requestAlwaysAuthorization()
}else if whenInUse != nil {
locationM.requestWhenInUseAuthorization()
// 1. 判断后台模式有没有勾选location updates
// 2. 判断当前版本时候是ios9.0, 因为只有在ios9.0以后, 才需要额外的设置一个属性为true
let backModes = infoDic["UIBackgroundModes"]
if backModes != nil {
let resultBackModel = backModes as! [String]
if resultBackModel.contains("location") {
if #available(iOS 9.0, *){
locationM.allowsBackgroundLocationUpdates = true
}
}
}
}else {
print("错误提示: 不要紧张 在ios8.0以后,想要使用用户位置, 你应该先在info.plist 配置NSLocationWhenInUseUsageDescription 或者 NSLocationAlwaysUsageDescription")
}
//5. 每隔100 米定位一次
locationM.distanceFilter = 100
}
return locationM
}()
获取坐标:
func getCurrentPointLocation(isOnce: Bool,resultPointBlock: @escaping LocationPointResultBlock) -> () {
self.isOnce = isOnce
// 1. 记录block
self.resultPointBlock = resultPointBlock
self.isLocation = true
// 2. 在合适的地方执行
if CLLocationManager.locationServicesEnabled() {
if isOnce == true {
// 单次定位请求
// 必须实现代理的定位失败方法
// 不能与startUpdatingLocation方法同时使用
if #available(iOS 9.0, *) {
locationM.requestLocation()
} else {
locationM.startUpdatingLocation()
}
}else{
locationM.startUpdatingLocation()
}
}else {
if self.resultPointBlock != nil {
self.resultPointBlock!(nil, "当前没有开启定位服务")
}
}
}
获取位置信息:
func getCurrentLocation(isOnce: Bool,resultBlock: @escaping LocationResultBlock) -> () {
self.isOnce = isOnce
// 1. 记录block
self.resultBlock = resultBlock
if locationCode != nil && self.resultBlock != nil {
resultBlock(locationCode, nil)
}
// 2. 在合适的地方执行
if CLLocationManager.locationServicesEnabled() {
if isOnce == true {
// 单次定位请求
// 必须实现代理的定位失败方法
// 不能与startUpdatingLocation方法同时使用
if #available(iOS 9.0, *) {
locationM.requestLocation()
} else {
locationM.startUpdatingLocation()
}
}else{
locationM.startUpdatingLocation()
}
}else {
if self.resultBlock != nil {
self.resultBlock!(nil, "当前没有开启定位服务")
}
}
}
调用很简单:
LocationUtil.share.getCurrentPointLocation(isOnce: false) { (loc, errorMsg) in
if errorMsg == nil {
self.localLabel.text = String(format: "%f,%f", (loc?.coordinate.latitude)!,(loc?.coordinate.longitude)!)
}
}
LocationUtil.share.getCurrentLocation(isOnce: false) { (loc, errorMsg) -> () in
if errorMsg == nil {
self.areaLabel.text = String(format: "%@", (loc?.name)!)
}
}
标注:返回CLPlacemark参数
// 位置名
NSLog(@"name,%@",loc.name);
// 街道
NSLog(@"thoroughfare,%@",loc.thoroughfare);
// 子街道
NSLog(@"subThoroughfare,%@",loc.subThoroughfare);
// 市
NSLog(@"locality,%@",loc.locality);
// 区
NSLog(@"subLocality,%@",loc.subLocality);
// 国家
NSLog(@"country,%@",loc.country);