zoukankan      html  css  js  c++  java
  • iOS CoreLocation定位服务

      CoreLocation导入框架  :#import <CoreLocation/CoreLocation.h>

      需要了解的基本的属性和方法:

      属性:

    • 定位管理者:CLLocationManager
    • 请求定位权限:requestAlwaysAuthorization
    • 开始获取位置:startUpdatingLocation
    • 停止获取位置:stopUpdatingLocation
    • 授权认证状态:CLAuthorizationStatus
    • 过滤定位的距离:distanceFilter
    • 定位所需精度:desiredAccuracy
    • 定位到的信息:CLLocation
    • 创建经纬度点:CLLocationCoordinate2D
    • 地理编码:CLGeocoder

        

      方法:

    • 授权状态发生改变:

      - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

    • 获取到位置信息:

      - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

    • 进入监听区域:

      - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

    • 离开监听区域:

      - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region

      

      CoreLocatio定位的基本操作:

      在ios8后,系统不会默认帮我们调用定位授权,需要我们自己主动要求用户给我们授权,我们需要调用此方法:

      [self.mgr requestAlwaysAuthorization];

      并且我们还需要在info.plist文件中配置:

      NSLocationWhenInUseDescription,允许在前台获取GPS的描述

         NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述

    #import "ViewController.h"
    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController ()<CLLocationManagerDelegate>
    /**
     *  定位管理者
     */
    @property (nonatomic ,strong) CLLocationManager *mgr;
    @end
    
    @implementation ViewController
    
    // 懒加载
    // 创建CoreLocation管理者
    - (CLLocationManager *)mgr
    {
        if (!_mgr) {
            _mgr = [[CLLocationManager alloc] init];
        }
        return _mgr;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 设置代理监听获取到的位置
        self.mgr.delegate = self;
    
        // 判断是否是iOS8
        if([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0)
        {
            NSLog(@"是iOS8");
            // 主动要求用户对我们的程序授权, 授权状态改变就会通知代理
            [self.mgr requestAlwaysAuthorization]; 
        }else
        {
           // 开始监听(开始获取位置)
            [self.mgr startUpdatingLocation];
        }
        
    }
    
    // 授权状态发生改变时调用
    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
    {
       if (status == kCLAuthorizationStatusNotDetermined) {
            NSLog(@"等待用户授权");
        }else if (status == kCLAuthorizationStatusAuthorizedAlways ||
                  status == kCLAuthorizationStatusAuthorizedWhenInUse)
            
        {
            NSLog(@"授权成功");
            // 开始定位
            [self.mgr startUpdatingLocation];
            
        }else
        {
            NSLog(@"授权失败");
        }
    }
    
    #pragma mark - CLLocationManagerDelegate
    //  获取到位置信息之后就会调用(调用频率非常高)
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        // 定位的信息
        // CLLocation *location = [locations lastObject];
       
        
    }

      地理编码(地址转经纬度):

     1 #import "ViewController.h"
     2 #import <CoreLocation/CoreLocation.h>
     3 
     4 @interface ViewController ()
     5 
     6 #pragma mark - 地理编码
     7  //  地理编码对象
     8 @property (nonatomic ,strong) CLGeocoder *geocoder;
     9 @end
    10 
    11 @implementation ViewController
    12 
    13 // 懒加载
    14 - (CLGeocoder *)geocoder
    15 {
    16     if (!_geocoder) {
    17         _geocoder = [[CLGeocoder alloc] init];
    18     }
    19     return _geocoder;
    20 }
    21 
    22 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event23 {
    24     // 位置
    25     NSString *addressStr = @"上海";
    26     
    27     // 利用地理编码对象编码
    28     // 根据传入的地址获取该地址对应的经纬度信息
    29     [self.geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
    30         
    31         if (placemarks.count == 0 ) {
    32             return ;
    33         }
    34         // placemarks地标数组, 地标数组中存放着地标, 每一个地标包含了该位置的经纬度以及城市/区域/国家代码/邮编等等...
    35         // 获取数组中的第一个地标
    36         CLPlacemark *placemark = [placemarks firstObject];
    37 //        for (CLPlacemark  *placemark in placemarks) {
    38 //            NSLog(@"%@ %@ %f %f", placemark.name, placemark.addressDictionary, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
    39 
    40        // 获取城市数组
    41         NSArray *address = placemark.addressDictionary[@"FormattedAddressLines"];
    42         NSMutableString *strM = [NSMutableString string];
    43         for (NSString *str in address) {
    44             [strM appendString:str];
    45         }
    46         
    47 //        }
    48   
    49     }];
    50 }

       

      反地理编码(经纬度转地址):

     1 #import "ViewController.h"
     2 #import <CoreLocation/CoreLocation.h>
     3 
     4 @interface ViewController ()
     5 
     6 // 地理编码对象
     7 @property (nonatomic ,strong) CLGeocoder *geocoder;
     8 
     9 @end
    10 
    11 @implementation ViewController
    12 
    13 #pragma mark - 懒加载
    14 - (CLGeocoder *)geocoder
    15 {
    16     if (!_geocoder) {
    17         _geocoder = [[CLGeocoder alloc] init];
    18     }
    19     return _geocoder;
    20 }
    21 
    22 
    23 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    24 {
    25     // 经纬度
    26     NSString *longtitude = @"122.48";
    27     NSString *latitude = @"32.22";
    28    
    29     // 根据经纬度创建CLLocation对象
    30     CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue]  longitude:[longtitude doubleValue]];
    31     
    32     // 根据CLLocation对象获取对应的地标信息
    33     [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
    34         
    35         for (CLPlacemark *placemark in placemarks) {
    36             NSLog(@"%@ %@ %f %f %@", placemark.name, placemark.addressDictionary, placemark.location.coordinate.latitude, placemark.location.coordinate,longitude,placemark.locality);
    37         }
    38     }];
    39 }
    40 
    41 @end
    欢迎加QQ群交流: iOS: 279096195 React Native: 482205185
  • 相关阅读:
    MVC模式简介
    UEditor插入表格没有边框但有间距
    MVC准备前基础知识
    如何关闭ie9烦人的提示信息?
    javaScript中利用ActiveXObject来创建FileSystemObject操作文件
    win7下IIS安装与配置运行网站
    javascript函数
    减小SSN影响
    EMC (电磁兼容性)
    电源完整性设计
  • 原文地址:https://www.cnblogs.com/GeekStar/p/4451074.html
Copyright © 2011-2022 走看看