zoukankan      html  css  js  c++  java
  • iOS CLLocationManager定位

    本文转载至 http://www.tuicool.com/articles/7JBRZn

    在iOS8以前的版本中,我们使用CLLocationManager定位是没有问题的,最近在iOS8系统中却无法定位了。。。。这是一大问题啊!

    1、首先定义一个全局的变量用来记录CLLocationManager对象,引入CoreLocation.framework使用#import <CoreLocation/CoreLocation.h>
    
    @property (nonatomic, strong) CLLocationManager  *locationManager;
    2、初始化CLLocationManager并开始定位
    locationManager=[[CLLocationManager alloc] init];
        locationManager.delegate=self;
        locationManager.desiredAccuracy=kCLLocationAccuracyBest;
        locationManager.distanceFilter=10; 
        [locationManager startUpdatingLocation];//开启定位
    3、实现CLLocationManagerDelegate的代理方法
    
    #pragma mark CLLocationManagerDelegate
    /**
    * 获取经纬度
    */
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
      CLLocation *currLocation=[locations lastObject];
      location.strLatitude=[NSString stringWithFormat:@"%f",currLocation.coordinate.latitude];
      location.strLongitude=[NSString stringWithFormat:@"%f",currLocation.coordinate.longitude];
      NSLog(@"la---%f, lo---%f",currLocation.coordinate.latitude,currLocation.coordinate.longitude);
    }
    /**
     *定位失败,回调此方法
     */
    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
      if ([error code]==kCLErrorDenied) {
        NSLog(@"访问被拒绝");
      }
      if ([error code]==kCLErrorLocationUnknown) {
        NSLog(@"无法获取位置信息");
      }
    }
    

    iOS8中使用CoreLocation定位

    1、在使用CoreLocation前需要调用如下函数【iOS8专用】:
    iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:
    (1)始终允许访问位置信息
    
    - (void)requestAlwaysAuthorization;
    (2)使用应用程序期间允许访问位置数据
    
    - (void)requestWhenInUseAuthorization;
       示例如下:
    locationManager=[[CLLocationManager alloc] init];
      locationManager.delegate=self;
      locationManager.desiredAccuracy=kCLLocationAccuracyBest;
      locationManager.distanceFilter=10;
      if (iOSVersion>=8) {
        [locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8定位需要)
      }
      [locationManager startUpdatingLocation];//开启定位
    
    2、在Info.plist文件中添加如下配置:
    (1)NSLocationAlwaysUsageDescription
    (2)NSLocationWhenInUseUsageDescription

    这样添加后,定位功能就能正常使用了!
  • 相关阅读:
    Python系列:5- Day1
    Python系列:4-计算机中的进制和编码
    操作系统随笔:什么是微内核和宏内核?【华为鸿鹄操作系统-微内核】
    Python系列:3-操作系统简史
    Python系列:2-电脑结构和CPU、内存、硬盘三者之间的关系
    数据结构中头结点和头指针那么易混淆吗
    pareto最优解(多目标智能算法要用到)
    Matlab学习中遇到的不熟悉的函数(智能算法学习第一天)
    6-2
    6-1
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/4212982.html
Copyright © 2011-2022 走看看