IOS入门笔记之地理位置定位系统
前言:关于地理位置及定位系统,在iOS开发中也比较常见,比如美团外面的餐饮店铺的搜索,它首先需要用户当前手机的位置,然后在这个位置附近搜索相关的餐饮店铺的位置,并提供相关的餐饮信息,再比如最常见的就是地图导航,地图导航更需要定位服务,然后根据用户的目的地选出一条路线。其实,作为手机用户这么长时间,或多或少会发现在有些app应用首次在你的手机安装成功后,首次启动可能就会提示"是否同意XXx(比如百度浏览器)获取当前位置"等这样一类的信息。可见地理位置及定位系统是企业app开发必不可少的技能。
本章将提供Swift版本和Objective-C两个版本的入门代码,分别实现显示当前手机或者是模拟器的地理经纬度坐标。
写在正式学习前的小贴士:
这是因为xcode升级造成的定位权限设置问题。
升级xcode6、xcode7以后打开以前xcode5工程,程序不能定位。工程升级到xcode6或xcode7编译时需要iOS8要自己写授权,不然没权限定位。
解决方法:
首先在info.plist里加入对应的缺省字段,值设置为YES(前台定位写上边字段,前后台定位写下边字段)
NSLocationWhenInUseUsageDescription//允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription//允许在前、后台获取GPS的描述
设置的图示:
好了,如果设置好了,那就正式进入编码学习吧,首先熟悉苹果提供的关于定位服务相关的类,方法以及属性:
1、定位服务和地图应用的介绍
定位服务:获取用户当前的位置信息,针对用户的位置信息做相关的数据处理。
地图应用:根据实际需求展示地图和周边环境信息,基于用户当前位置展示用户所关注的地图位置信息、以及为用户导航。
•定位服务要掌握的:
•主要操作的类:CLLocationManager
•所属库:CoreLocation
•结构体:CLLocationCoordinate2D(经纬度)、CLCLocationCoorRegion(区域)
•地图应用需要掌握的:
•框架:MapKit
•操作类:MKMapView
2、定位服务
•属性:
•desiredAccuracy设置定位精确度,这是一个常量属性,一般用best
•distanceFilter重新定位的最小变化距离
方法:
•设置什么时候开启定位的状态•requestAlwaysAuthorization()始终开启定位
•requestWhenInUseAuthorization()当app进入前台的时候开启定位(iOS8的新方法)
•类方法locationServicesEnabled()是否有定位服务功能(CLLocationManager)
•startUpdatingLocation()开启定位
代理:
•代理的协议:
•代理的方法:可以直接进入这个库的API查看,只要就是定位错误调用的代理方法,定位成功调用的代理方法等等;
涉及到的对象
•locations:CLLocation该CLLocation对象的属性:•coordinate•longitude/latitude
英语词汇积累:
•accuracy英'ækjʊrəsɪn.[数]精确度,准确性
•filter英'fɪltə滤波器过滤器;筛选;滤光器过滤;渗透;用过滤法除去
下面提供的是Swift源码:
// //ViewController.swift //LocationManager // //CreatedbyHEYANGon//. //Copyright©年HEYANG.Allrightsreserved. // importUIKit //需要导入CoreLocation框架 importCoreLocation classViewController:UIViewController,CLLocationManagerDelegate{ //声明一个全局变量 varlocationManager:CLLocationManager! overridefuncviewDidLoad(){ super.viewDidLoad() locationManager=CLLocationManager() //设置定位的精确度 locationManager.desiredAccuracy=kCLLocationAccuracyBest //设置定位变化的最小距离距离过滤器 locationManager.distanceFilter= //设置请求定位的状态 if#available(iOS.,*){ locationManager.requestWhenInUseAuthorization() }else{ //Fallbackonearlierversions print("hello") }//这个是在ios之后才有的 //设置代理为当前对象 locationManager.delegate=self; ifCLLocationManager.locationServicesEnabled(){ //开启定位服务 locationManager.startUpdatingLocation() }else{ print("没有定位服务") } } //定位失败调用的代理方法 funclocationManager(manager:CLLocationManager,didFailWithErrorerror:NSError){ print(error) } //定位更新地理信息调用的代理方法 funclocationManager(manager:CLLocationManager,didUpdateLocationslocations:[CLLocation]){ iflocations.count> { letlocationInfo=locations.last! letalert:UIAlertView=UIAlertView(title:"获取的地理坐标", message:"经度是:\(locationInfo.coordinate.longitude),维度是:\(locationInfo.coordinate.latitude)", delegate:nil,cancelButtonTitle:"是的") alert.show() } } }
下面是Objective-C的源码:
// //ViewController.m //LocationManager // //CreatedbyHEYANGon//. //Copyright©年HEYANG.Allrightsreserved. // #import"ViewController.h" #import<CoreLocation/CoreLocation.h> @interfaceViewController()<CLLocationManagerDelegate> /**全局定位对象*/ @property(nonatomic,strong)CLLocationManager*locationManager; @end @implementationViewController -(void)viewDidLoad{ [superviewDidLoad]; CLLocationManager*locationManager=[[CLLocationManageralloc]init]; //设置定位精确度 locationManager.desiredAccuracy=kCLLocationAccuracyBest; //设置定位变化最小距离 locationManager.distanceFilter=; //设置定位服务的使用状态 [locationManagerrequestWhenInUseAuthorization]; locationManager.delegate=self; if([CLLocationManagerlocationServicesEnabled]){ [locationManagerstartUpdatingLocation]; }else{ NSLog(@"本机不支持定位服务功能"); } self.locationManager=locationManager; } //定位失败调用的代理方法 -(void)locationManager:(CLLocationManager*)managerdidFailWithError:(NSError*)error{ NSLog(@"错误信息:%@",error); } //定位数据更新调用的代理方法 -(void)locationManager:(CLLocationManager*)managerdidUpdateLocations:(NSArray<CLLocation*>*)locations{ if(locations.count>){ CLLocation*location=locations.lastObject; CLLocationCoordinateDcoordinateD=location.coordinate; NSString*message=[NSStringstringWithFormat:@"经度:%lf,维度是:%lf",coordinateD.longitude,coordinateD.latitude]; UIAlertView*alertView=[[UIAlertViewalloc]initWithTitle:@"显示当前位置的经纬度"message:messagedelegate:nilcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil]; [alertViewshow]; } } @end
以上是小编给大家分享的IOS入门笔记之地理位置定位系统,希望对大家有所帮助。