iOS开发 CoreLocation实现定位服务

iOS开发 CoreLocation实现定位服务
听语音

  • |
  • 浏览:111
  • |
  • 更新:2016-06-08 18:39
  • |
  • 标签:IOS 
  • iOS开发 CoreLocation实现定位服务 1
  • iOS开发 CoreLocation实现定位服务 2
  • iOS开发 CoreLocation实现定位服务 3
  • iOS开发 CoreLocation实现定位服务 4
  • iOS开发 CoreLocation实现定位服务 5
  • iOS开发 CoreLocation实现定位服务 6
  • iOS开发 CoreLocation实现定位服务 7
分步阅读

     CoreLocation是iOS SDK自带核心定位框架。是利用GPS定位,获取经纬度、速度和地理位置信息。需要注意的是适配iOS8,可以设置精度desiredAccuracy、设置移动多少距离后更新位置distanceFilter。定位地理位置信息如图,图片效果是把控制台的输出内容(Unicode)编码转换成中文的。代码在github的CoreLocation上。

iOS开发 CoreLocation实现定位服务

工具/原料

  • Mac OS X操作系统:OS X 10.11.5 
  • 编译环境:Xcode 7.3.1

方法/步骤

  1. 创建工程项目和视图控制器

          1、创建一个Sing View Application工程项目;

          2、为项目命名,生成工程文件。

    iOS开发 CoreLocation实现定位服务











  2. 为适配iOS8需要配置info.plist文件

          添加2行:

          NSLocationAlwaysUsageDescription 设为Boolean类型 = YES

          NSLocationWhenInUseUsageDescription 设为Boolean类型 = YES 

    iOS开发 CoreLocation实现定位服务











  3. 引入CoreLocation框架

          包含头文件:#import <CoreLocation/CoreLocation.h>

          引用代理:CLLocationManagerDelegate

          声明定位管理器: CLLocationManager *locationManager;

    iOS开发 CoreLocation实现定位服务












  4. 初始化对象

        self.locationManager = [[CLLocationManager alloc] init];

        self.locationManager.delegate = self;

        self.locationManager.distanceFilter = 1.0;

        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        

        if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])

        {

            [self.locationManager requestAlwaysAuthorization]; // 永久授权

            [self.locationManager requestWhenInUseAuthorization]; //使用中授权

        }

    iOS开发 CoreLocation实现定位服务





  5. 实现定位代理更新位置成功回调

          - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

    {

        NSLog(@"经度:%f", newLocation.coordinate.longitude);

        NSLog(@"纬度:%f", newLocation.coordinate.latitude);

        NSLog(@"速度:%f 米/秒", newLocation.speed);

        CLGeocoder * geocoder = [[CLGeocoder alloc] init];

        [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {

            

            NSDictionary *locationInfo = [[NSDictionary alloc]init];

            for (CLPlacemark * placemark in placemarks) {

                locationInfo = [placemark addressDictionary];

            }

            NSLog(@"%@",locationInfo);

        }];

    }

    iOS开发 CoreLocation实现定位服务









  6. 定位代理失败回调

          - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

    {

        NSLog(@"%@", error);

    }

    iOS开发 CoreLocation实现定位服务

  7. 开启/停止位置更新

         

        开启:[self.locationManager startUpdatingLocation];

          停止:[self.locationManager stopUpdatingLocation];

    在viewDidLoad方法里面开启定位更新服务。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值