zoukankan      html  css  js  c++  java
  • iOS 8 开发运用系统自带CoreLocation 实现定位功能

    运用系统自带的CoreLocation实现手机定位功能

    .h

    1.首先导入系统类库

    #import <CoreLocation/CoreLocation.h>

    2,创建CllocationManager 对象

    {

    CllocationManager * _locationManager;

    }

    .m

    1.在.m中实现CllocationManager对象

    self._locationManager = [[CllocationManager alloc]init];

    2.// 设置代理

    self._locationManager.delegate = self; 

    3.设置精确度

    self._locationManager.desiredAccuracy = KCLLocationAccuracyBest;

    4.设置它的distanceFilter

    self._locationManager.distanceFilter = 10.0f;

    5.设置始终允许访问位置信息

    [self._locationManager requestAlwaysAuthorization];

    6.开始定位

    [self._locationManager startUpdatingLocation];

    7.实现协议方法

     1 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
     2 {
     3 //进行地理编码
     4 CLGeocoder * geocoder = [[CLGeocoder alloc]init];
     5 //因为我这里是模拟器 所以使用的是[locations lastObject]  如果是真机得话可以试一下[locations lastObject]和[locations firstObject]的区别
     6 [geocoder reverseGeocodeLocation:[locations lastObject] completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error){
     7   for (CLPlacemark * placemark in placemarks) {
     8             NSDictionary * test = [placemark addressDictionary];
     9             [self._localtionManager stopUpdatingLocation];
    10             NSLog(@"%@", [test objectForKey:@"State"]);
    11 }];
    12 };
    13 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    14 {
    15     if ([error code] == kCLErrorDenied) {
    16         NSLog(@"访问被拒");
    17     }
    18     if ([error code] == kCLErrorLocationUnknown) {
    19         NSLog(@"无法获取位置信息");
    20     }
    21 }

    注意: 在ios8上运用CllocationManager进行定位的话,需要对.plist 文件进行配置 需要在.plist文件里面添加如下;

    1,

    NSLocationAlwaysUsageDescription

    2,

    NSLocationWhenInUseUsageDescription

    添加完成之后就可以进行定位操作了.....祝大家生活愉快!!!!

  • 相关阅读:
    <a>标签实现锚点跳跃,<a>标签实现href不跳跃另外加事件(ref传参)
    ThinkPHP实现事务回滚示例代码(附加:PDO的事务处理)
    python 命令执行文件传递参数
    python os.walk()
    python sys.stdin、sys.stdout和sys.stderr
    Python 为什么sys.stdout.write 输出时后面总跟一个数字
    python 不同集合上元素的迭代 chain()
    Python zip() 处理多于两个序列的参数, 存储结对的值
    Python 成对处理数据 zip()
    python 同时迭代多个序列
  • 原文地址:https://www.cnblogs.com/JustForHappy/p/4920468.html
Copyright © 2011-2022 走看看