iOS中的地理位置的获取及plist设置方法
1、在前台的时候获取地理位置信息
ios 8/9
在info.plist中配置nslocationwheninuseusagedescription的值,否则上面的方法无效
调用.requestwheninuseauthorization()获取前台获取地理位置权限
调用.startupdatinglocation()
代码示例
class viewcontroller: uiviewcontroller { lazy var locatem : cllocationmanager = { let locate = cllocationmanager() locate.delegate = self locate.requestwheninuseauthorization() return locate }() override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) { self.locatem.startupdatinglocation() } } extension viewcontroller : cllocationmanagerdelegate{ func locationmanager(manager: cllocationmanager, didupdatelocations locations: [cllocation]) { print("位置信息已经更新") } }
2、前后台获取,但是后台获取的时候,屏幕上方有蓝框提示用户正在后台获取
ios8
调用.requestwheninuseauthorization()获取前台获取地理位置权限
在info.plist中配置nslocationwheninuseusagedescription的值,否则上面的方法无效
设置capabilities>backgroundmodes>location updates 打对勾
调用.startupdatinglocation()
ios9
调用.requestwheninuseauthorization()获取前台获取地理位置权限
设置 .allowsbackgroundlocationupdates = true (ios 9需要执行)
在info.plist中配置nslocationwheninuseusagedescription的值,否则上面的方法无效
设置capabilities>backgroundmodes>location updates 打对勾 (如果第二步做了,此步没做,直接crash)
调用.startupdatinglocation()
ios8/ios9可以后台蓝框定位的代码示例:
class viewcontroller: uiviewcontroller { lazy var locatem : cllocationmanager = { let locate = cllocationmanager() locate.delegate = self locate.requestwheninuseauthorization() if #available(ios 9.0, *) { locate.allowsbackgroundlocationupdates = true } return locate }() override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) { self.locatem.startupdatinglocation() } } extension viewcontroller : cllocationmanagerdelegate{ func locationmanager(manager: cllocationmanager, didupdatelocations locations: [cllocation]) { print("位置信息已经更新") } }
3、后台获取,后台获取的时候,屏幕上方无蓝框提示
调用.requestalwaysauthorization()获取前台获取地理位置权限
在info.plist中配置nslocationalwaysusagedescription的值,否则上面的方法无效
设置 .allowsbackgroundlocationupdates = true (ios 9需要执行)
设置capabilities>backgroundmodes>location updates 打对勾 (本步骤在ios 8中可以不做设置,但是在ios9中如果第三步做了,而此步没有做,直接crash)
调用.startupdatinglocation()
代码示例
class viewcontroller: uiviewcontroller { lazy var locatem : cllocationmanager = { let locate = cllocationmanager() locate.delegate = self locate.requestalwaysauthorization() if #available(ios 9.0, *) { locate.allowsbackgroundlocationupdates = true } return locate }() override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) { self.locatem.startupdatinglocation() } } extension viewcontroller : cllocationmanagerdelegate{ func locationmanager(manager: cllocationmanager, didupdatelocations locations: [cllocation]) { print("位置信息已经更新") } }
4、权限改变的通知
注意:在denied或者notdetermined的状态下startupdatinglocation,开始监听之后,当状态改变成允许的状态时,会直接进入监听状态,不必再次调用startupdateinglocation
func locationmanager(manager: cllocationmanager, didchangeauthorizationstatus status: clauthorizationstatus) { switch status { case .authorizedalways: print("始终") case .authorizedwheninuse: print("使用的时候") case .denied: print("拒绝") if cllocationmanager.locationservicesenabled() { print("真拒绝了") }else{ print("是关闭了定位服务") } case .notdetermined: print("第一次,尚未决定") case .restricted: print("没有权限的") } }
5、过滤距离
很多时候我们需要监听函数只调用一次来获取用户当前的位置
在监听函数中停止监听
设置监听的过滤距离
//如果监听器已经开启,此值修改之后立即生效 self.locatem.distancefilter = 100 //每100米,调用一次监听
6、精度
注意:越精确越耗电,定位的时间越长,如果要定位城市,没有必要选最精确的
self.locatem.desiredaccuracy = kcllocationaccuracybest //kcllocationaccuracybestfornavigation //kcllocationaccuracybest //kcllocationaccuracynearesttenmeters //kcllocationaccuracyhundredmeters //kcllocationaccuracykilometer //kcllocationaccuracythreekilometers
7.cllocation详解
public var coordinate: cllocationcoordinate2d { get } //经纬度 public var altitude: cllocationdistance { get } //海拔 public var horizontalaccuracy: cllocationaccuracy { get } //位置信息是否有效,如果为负数,则无效 public var verticalaccuracy: cllocationaccuracy { get } //海拔数据是否有效,如果为负数,则无效 public var course: cllocationdirection { get } //当前的角度(0-359.9) public var speed: cllocationspeed { get } //当前的速度 public var timestamp: nsdate { get } //位置确定的时间戳 public var floor: clfloor? { get } //楼层(前提是已经注册的建筑),如果没有为nil //计算两个经纬度之间的距离 public func distancefromlocation(location: cllocation) -> cllocationdistance
8、指南针小例子
class viewcontroller: uiviewcontroller { @iboutlet weak var mimageview: uiimageview! lazy var locatem : cllocationmanager = { let locate = cllocationmanager() locate.delegate = self locate.requestalwaysauthorization() if #available(ios 9.0, *) { locate.allowsbackgroundlocationupdates = true } return locate }() override func viewdidload() { super.viewdidload() if(cllocationmanager.headingavailable()){ self.locatem.startupdatingheading() }else{ print("当前磁力计有问题") } } } extension viewcontroller : cllocationmanagerdelegate{ func locationmanager(manager: cllocationmanager, didupdateheading newheading: clheading) { //1.拿到当前设备对正朝向的角度 let angle = newheading.magneticheading //2.把角度转换成弧度 let hudu = cgfloat(angle / 180 * m_pi) //3.反向旋转照片 uiview.animatewithduration(0.5) { self.mimageview.transform = cgaffinetransformmakerotation(-hudu) } } }
9、区域的监听
class viewcontroller: uiviewcontroller { lazy var locatem : cllocationmanager = { let locate = cllocationmanager() locate.delegate = self locate.requestalwaysauthorization() if #available(ios 9.0, *) { locate.allowsbackgroundlocationupdates = true } return locate }() override func viewdidload() { super.viewdidload() //首先应该判断当前是否可以监听某个区域 if cllocationmanager.ismonitoringavailableforclass(clcircularregion){ //1.创建区域 let center = cllocationcoordinate2dmake(21.123, 121.345) var distance : cllocationdistance = 1000 //限制监听的范围不能超过最大的范围 if distance < locatem.maximumregionmonitoringdistance{ distance = locatem.maximumregionmonitoringdistance } let region = clcircularregion(center: center, radius: distance, identifier: "xiaoxiao") //2.监听区域 self.locatem.startmonitoringforregion(region) //3.判断当前状态是否是在区域内还是区域外, //在`diddeterminestate`代理方法中获得结果 self.locatem.requeststateforregion(region) } } } extension viewcontroller : cllocationmanagerdelegate{ func locationmanager(manager: cllocationmanager, didenterregion region: clregion) { print("进入了区域"+region.identifier) } func locationmanager(manager: cllocationmanager, didexitregion region: clregion) { print("出了区域"+region.identifier) } func locationmanager(manager: cllocationmanager, diddeterminestate state: clregionstate, forregion region: clregion) { //获取刚开始是否在区域内或者区域外 if region.identifier == "xiaoxiao"{ switch state { case .inside: print("已经是区域内的") case .outside: print("没有在区域内") case .unknown: print("不清楚") } } } }
10、地理编码与反地理编码
地理编码
let geocoder = clgeocoder() geocoder.geocodeaddressstring("广州") { (pls:[clplacemark]?, error : nserror?) in if error == nil{ print("地址编码成功") print(pls?.last?.location) }else{ print("错误 \(error)") } }
打印
地址编码成功
optional(<+23.12517800,+113.28063700> +/- 100.00m (speed -1.00 mps / course -1.00) @ 8/14/16, 9:49:22 pm china standard time)
反地理编码
let geocoder = clgeocoder() geocoder.reversegeocodelocation(cllocation(latitude:23.125,longitude: 113.280)) { (pls:[clplacemark]?, error:nserror?) in if error == nil{ print("地址反编码成功 城市:\(pls?.last?.locality)") print(pls?.last?.addressdictionary) }else{ print("错误 \(error)") } }
打印
地址反编码成功 城市:optional("guangzhou")
optional([sublocality: yuexiu, street: yunhai tongjin no.11, state: guangdong, countrycode: cn, thoroughfare: yunhai tongjin no.11, name: luo sangmeidi, country: china, formattedaddresslines: <__nsarraym 0x7ff1da5652d0>( yunhai tongjin no.11 yuexiu, guangzhou, guangdong china ) , city: guangzhou])
注意同一个clgeocoder对象,不能同时编码与反编码
比如
let geocoder = clgeocoder() geocoder.geocodeaddressstring("广州") { (pls:[clplacemark]?, error : nserror?) in ... } geocoder.reversegeocodelocation(cllocation(latitude:23.125,longitude: 113.280)) { (pls:[clplacemark]?, error:nserror?) in ... }
这样只会打印第一个编码成功的结果
11、clplacemark对象详解
@nscopying public var location: cllocation? { get } //经纬度 @nscopying public var region: clregion? { get } //所关联的地理区域 @available(ios 9.0, *) @nscopying public var timezone: nstimezone? { get } //时间域 public var addressdictionary: [nsobject : anyobject]? { get } //详细地址信息 //addressdictionary中的属性 public var name: string? { get } //名字 public var thoroughfare: string? { get } //街道名字 public var subthoroughfare: string? { get } //子街道名字 public var locality: string? { get } //城市名称 public var sublocality: string? { get } //邻城市名称 public var administrativearea: string? { get } //行政区域 比如:ca public var subadministrativearea: string? { get } //子行政区域 public var postalcode: string? { get } //邮政编码 public var isocountrycode: string? { get } //国家代码表 public var country: string? { get } //国家 public var inlandwater: string? { get } //内陆水域 public var ocean: string? { get } //海洋 public var areasofinterest: [string]? { get } //兴趣点
以上这篇ios中的地理位置的获取及plist设置方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。