iOS中的地理位置的获取及plist设置方法
1、在前台的时候获取地理位置信息
ios8/9
在info.plist中配置NSLocationWhenInUseUsageDescription的值,否则上面的方法无效
调用.requestWhenInUseAuthorization()获取前台获取地理位置权限
调用.startUpdatingLocation()
代码示例
classViewController:UIViewController{ lazyvarlocateM:CLLocationManager={ letlocate=CLLocationManager() locate.delegate=self locate.requestWhenInUseAuthorization() returnlocate }() overridefunctouchesBegan(touches:Set,withEventevent:UIEvent?){ self.locateM.startUpdatingLocation() } } extensionViewController:CLLocationManagerDelegate{ funclocationManager(manager:CLLocationManager,didUpdateLocationslocations:[CLLocation]){ print("位置信息已经更新") } }
2、前后台获取,但是后台获取的时候,屏幕上方有蓝框提示用户正在后台获取
ios8
调用.requestWhenInUseAuthorization()获取前台获取地理位置权限
在info.plist中配置NSLocationWhenInUseUsageDescription的值,否则上面的方法无效
设置Capabilities>BackgroundModes>LocationUpdates打对勾
调用.startUpdatingLocation()
ios9
调用.requestWhenInUseAuthorization()获取前台获取地理位置权限
设置.allowsBackgroundLocationUpdates=true(ios9需要执行)
在info.plist中配置NSLocationWhenInUseUsageDescription的值,否则上面的方法无效
设置Capabilities>BackgroundModes>LocationUpdates打对勾(如果第二步做了,此步没做,直接crash)
调用.startUpdatingLocation()
ios8/ios9可以后台蓝框定位的代码示例:
classViewController:UIViewController{ lazyvarlocateM:CLLocationManager={ letlocate=CLLocationManager() locate.delegate=self locate.requestWhenInUseAuthorization() if#available(iOS9.0,*){ locate.allowsBackgroundLocationUpdates=true } returnlocate }() overridefunctouchesBegan(touches:Set,withEventevent:UIEvent?){ self.locateM.startUpdatingLocation() } } extensionViewController:CLLocationManagerDelegate{ funclocationManager(manager:CLLocationManager,didUpdateLocationslocations:[CLLocation]){ print("位置信息已经更新") } }
3、后台获取,后台获取的时候,屏幕上方无蓝框提示
调用.requestAlwaysAuthorization()获取前台获取地理位置权限
在info.plist中配置NSLocationAlwaysUsageDescription的值,否则上面的方法无效
设置.allowsBackgroundLocationUpdates=true(ios9需要执行)
设置Capabilities>BackgroundModes>LocationUpdates打对勾(本步骤在ios8中可以不做设置,但是在ios9中如果第三步做了,而此步没有做,直接crash)
调用.startUpdatingLocation()
代码示例
classViewController:UIViewController{ lazyvarlocateM:CLLocationManager={ letlocate=CLLocationManager() locate.delegate=self locate.requestAlwaysAuthorization() if#available(iOS9.0,*){ locate.allowsBackgroundLocationUpdates=true } returnlocate }() overridefunctouchesBegan(touches:Set,withEventevent:UIEvent?){ self.locateM.startUpdatingLocation() } } extensionViewController:CLLocationManagerDelegate{ funclocationManager(manager:CLLocationManager,didUpdateLocationslocations:[CLLocation]){ print("位置信息已经更新") } }
4、权限改变的通知
注意:在Denied或者NotDetermined的状态下startUpdatingLocation,开始监听之后,当状态改变成允许的状态时,会直接进入监听状态,不必再次调用startUpdateingLocation
funclocationManager(manager:CLLocationManager,didChangeAuthorizationStatusstatus:CLAuthorizationStatus){ switchstatus{ case.AuthorizedAlways: print("始终") case.AuthorizedWhenInUse: print("使用的时候") case.Denied: print("拒绝") ifCLLocationManager.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详解
publicvarcoordinate:CLLocationCoordinate2D{get}//经纬度 publicvaraltitude:CLLocationDistance{get}//海拔 publicvarhorizontalAccuracy:CLLocationAccuracy{get}//位置信息是否有效,如果为负数,则无效 publicvarverticalAccuracy:CLLocationAccuracy{get}//海拔数据是否有效,如果为负数,则无效 publicvarcourse:CLLocationDirection{get}//当前的角度(0-359.9) publicvarspeed:CLLocationSpeed{get}//当前的速度 publicvartimestamp:NSDate{get}//位置确定的时间戳 publicvarfloor:CLFloor?{get}//楼层(前提是已经注册的建筑),如果没有为nil //计算两个经纬度之间的距离 publicfuncdistanceFromLocation(location:CLLocation)->CLLocationDistance
8、指南针小例子
classViewController:UIViewController{ @IBOutletweakvarmImageView:UIImageView! lazyvarlocateM:CLLocationManager={ letlocate=CLLocationManager() locate.delegate=self locate.requestAlwaysAuthorization() if#available(iOS9.0,*){ locate.allowsBackgroundLocationUpdates=true } returnlocate }() overridefuncviewDidLoad(){ super.viewDidLoad() if(CLLocationManager.headingAvailable()){ self.locateM.startUpdatingHeading() }else{ print("当前磁力计有问题") } } } extensionViewController:CLLocationManagerDelegate{ funclocationManager(manager:CLLocationManager,didUpdateHeadingnewHeading:CLHeading){ //1.拿到当前设备对正朝向的角度 letangle=newHeading.magneticHeading //2.把角度转换成弧度 lethudu=CGFloat(angle/180*M_PI) //3.反向旋转照片 UIView.animateWithDuration(0.5){ self.mImageView.transform=CGAffineTransformMakeRotation(-hudu) } } }
9、区域的监听
classViewController:UIViewController{ lazyvarlocateM:CLLocationManager={ letlocate=CLLocationManager() locate.delegate=self locate.requestAlwaysAuthorization() if#available(iOS9.0,*){ locate.allowsBackgroundLocationUpdates=true } returnlocate }() overridefuncviewDidLoad(){ super.viewDidLoad() //首先应该判断当前是否可以监听某个区域 ifCLLocationManager.isMonitoringAvailableForClass(CLCircularRegion){ //1.创建区域 letcenter=CLLocationCoordinate2DMake(21.123,121.345) vardistance:CLLocationDistance=1000 //限制监听的范围不能超过最大的范围 ifdistance10、地理编码与反地理编码
地理编码
letgeoCoder=CLGeocoder() geoCoder.geocodeAddressString("广州"){(pls:[CLPlacemark]?,error:NSError?)in iferror==nil{ print("地址编码成功") print(pls?.last?.location) }else{ print("错误\(error)") } }打印
地址编码成功
Optional(<+23.12517800,+113.28063700>+/-100.00m(speed-1.00mps/course-1.00)@8/14/16,9:49:22PMChinaStandardTime)反地理编码
letgeoCoder=CLGeocoder() geoCoder.reverseGeocodeLocation(CLLocation(latitude:23.125,longitude:113.280)){(pls:[CLPlacemark]?,error:NSError?)in iferror==nil{ print("地址反编码成功城市:\(pls?.last?.locality)") print(pls?.last?.addressDictionary) }else{ print("错误\(error)") } }打印
地址反编码成功城市:Optional("Guangzhou")
Optional([SubLocality:Yuexiu,Street:YunhaiTongjinNo.11,State:Guangdong,CountryCode:CN,Thoroughfare:YunhaiTongjinNo.11,Name:LuoSangmeidi,Country:China,FormattedAddressLines:<__NSArrayM0x7ff1da5652d0>( YunhaiTongjinNo.11Yuexiu, Guangzhou, GuangdongChina ) ,City:Guangzhou])注意同一个CLGeocoder对象,不能同时编码与反编码
比如
letgeoCoder=CLGeocoder() geoCoder.geocodeAddressString("广州"){(pls:[CLPlacemark]?,error:NSError?)in ... } geoCoder.reverseGeocodeLocation(CLLocation(latitude:23.125,longitude:113.280)){(pls:[CLPlacemark]?,error:NSError?)in ... }这样只会打印第一个编码成功的结果
11、CLPlacemark对象详解
@NSCopyingpublicvarlocation:CLLocation?{get}//经纬度 @NSCopyingpublicvarregion:CLRegion?{get}//所关联的地理区域 @available(iOS9.0,*) @NSCopyingpublicvartimeZone:NSTimeZone?{get}//时间域 publicvaraddressDictionary:[NSObject:AnyObject]?{get}//详细地址信息 //addressDictionary中的属性 publicvarname:String?{get}//名字 publicvarthoroughfare:String?{get}//街道名字 publicvarsubThoroughfare:String?{get}//子街道名字 publicvarlocality:String?{get}//城市名称 publicvarsubLocality:String?{get}//邻城市名称 publicvaradministrativeArea:String?{get}//行政区域比如:CA publicvarsubAdministrativeArea:String?{get}//子行政区域 publicvarpostalCode:String?{get}//邮政编码 publicvarISOcountryCode:String?{get}//国家代码表 publicvarcountry:String?{get}//国家 publicvarinlandWater:String?{get}//内陆水域 publicvarocean:String?{get}//海洋 publicvarareasOfInterest:[String]?{get}//兴趣点以上这篇iOS中的地理位置的获取及plist设置方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。