本文地址:http://blog.csdn.net/zhenggaoxing/article/details/42527225
1 定位,主要是干啥呢?
1 获取位置相关数据
2 获取位置相关信息
由此,我想到了当年玩反恐精英里面用瞄准镜的瞄准的感觉:
2 那……怎样定位呢?
1首先你得瞄准吧
这里解释一下:请求获取授权有两种情况:
- locationManager=[[CLLocationManager alloc]init]; // 定义locationManage对象 打开瞄准镜
- locationManager.delegate=self; // 实现代理 自己打枪
- [locationManager requestAlwaysAuthorization ]; // 请求永久授权 你可以持枪多久?
- locationManager.desiredAccuracy=kCLLocationAccuracyBest; //设置定位精度 调节面准镜远近
- //authorization 授权,认可
- locationManager.distanceFilter=1000.0f; //设置获得移动信息最小距离 目标走多远我需要调整?
1 requestAlwaysAuthorization 获取永久授权2 requestWhenInUseAuthorization 当使用的时候授权
设置精度有六种选择:1 kCLLocationAccuracyBestForNavigation 导航的最高精度,一般车载导航用(需外接电源)
2 kCLLocationAccuracyBest; 电池使用时候最高精度
3 kCLLocationAccuracyNearestTenMeters; 精确到10米
4 kCLLocationAccuracyHundredMeters; 精确到100米
5 kCLLocationAccuracyKilometer;精确到1000米
6 kCLLocationAccuracyThreeKilometers; 精确到3千米
2 你要把子弹上膛吧,打完了子弹得卸下来吧?
- -(void)viewDidAppear:(BOOL)animated
- {
- [locationManager startUpdatingLocation];// 开始更新位置信息 子弹上膛
- }
- -(void)viewDidDisappear:(BOOL)animated
- {
- [locationManager stopUpdatingLocation]; // 结束更新位置信息 卸下子弹
- }
3 接下来可以打枪了吧(实现委托方法)
- /*---------------------------locationmanagerdelegate的方法----------------------------------*/
- // 小译:-()位置管理员:某位置管理员 已经更新的位置信息:某位置信息
- -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
- {
- CLLocation *currentLocation=[locations lastObject];
- longtitudeTextField.text=[NSString stringWithFormat:@"%3.5f",currentLocation.coordinate.longitude];
- latitudeTextField.text=[NSString stringWithFormat:@"%3.5f",currentLocation.coordinate.latitude];
- highTextField.text=[NSString stringWithFormat:@"%3.5f",currentLocation.altitude];
- }
4 这万一,没打中呢?(协议方法)
- // 调用失败
- // 小译:-()位置管理员:某管理员 已经失败的错误信息:某错误信息
- -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
- {
- // 小译:deny拒绝
- if(error.code==kCLErrorDenied)
- {
- NSLog(@"拒绝访问");
- }else if (error.code==kCLErrorLocationUnknown)
- {
- NSLog(@"位置信息未知");
- }
- }
3 但是,这定位你想定就可以吗?(小心人家不让)
1要在info.list里面添加两个键哦
NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription2记得右键选择下面的选项
3 效果呢?(设置里面出现选项)
![]()
4 运行结果
5 源代码
https://git.oschina.net/zhengaoxing/location1selflocation