美文网首页
iOS-定位功能

iOS-定位功能

作者: 亦晴工作室 | 来源:发表于2016-08-25 11:15 被阅读59次
//定位功能
    //1.配置库 更改plist文件
    //info.plist   NSLocationWhenInUseUsageDescription  设置你的提示
    // CoreLocation.framework 核心位置框架
    //  #import <CoreLocation/CoreLocation.h>
    
    //2.定位(获取经纬度信息)
    _manager = [[CLLocationManager alloc] init];
    _manager.delegate = self;
    [self testLocation];
    
    //3.获取方向服务
    //[self testHeading];
    
    
    //地址编码和地址反编码
    [self testGeoCoder];
}


//地址编码和地址反编码
-(void)testGeoCoder
{
    //通过地理地址获取经纬度 ----- 地址编码(地理地址 ==> 经纬度)
    //注意: 国家标准规定, 涉及到地图的经纬度都要经过加密
    //      苹果定位定位得到的地址不能直接用在苹果地图中, 称为火星地址


    
    //地理编码  理解为经纬度和地理信息转换器
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    //将字符串转换成详细位置信息
    [geoCoder geocodeAddressString:@"宝盛路" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        for(CLPlacemark *placeMark in placemarks)
        {
            //            NSLog(@"____位置信息:%@",placeMark);
            NSLog(@"地址编码%@ %@ %@ %f %f",placeMark.country,placeMark.locality,placeMark.subLocality,placeMark.location.coordinate.longitude,placeMark.location.coordinate.latitude);
        }
    }];
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:40 longitude:116];
    //地址反编码  通过经纬度获取 地理位置
    //获取我的位置
    CLGeocoder *reverseGeoCoder = [[CLGeocoder alloc] init];
    [reverseGeoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        for(CLPlacemark *placemark in placemarks)
        {
            NSLog(@"反编码____位置信息%@",placemark);
            NSLog(@"地址反编码--->   %@",placemark.name);
        }
//        NSLog(@"%@",error);
    }];
}


//获取方向
-(void)testHeading
{
    if(![CLLocationManager headingAvailable])
    {
        NSLog(@"方向服务不可用");
        return;
    }
    //开启方向服务
    [_manager startUpdatingHeading];
}




-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    //0.0 - 359.9 degrees, 0 being true North
    NSLog(@"地理方向 %f",newHeading.trueHeading);
    NSLog(@"地磁方向 %f",newHeading.magneticHeading);
    // 360/16 = 22.5
    NSLog(@"%d",(int)(newHeading.trueHeading/22.50));
}


//实现定位
-(void)testLocation
{
    //检测定位服务是否可用
    if(![CLLocationManager locationServicesEnabled])
    {
        NSLog(@"定位服务不可用");
        return;
    }
    
    //请求定位权限(xcode6和iOS8中使用的)
    if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined)
    {
        [_manager requestAlwaysAuthorization];
    }
    
    //设置
    //设置距离变化时候通知的敏感程度  指定的最小更新米的距离  默认是none  不提醒
//    _manager.distanceFilter = 10;
    //设置希望定位的精度
    //_manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    //开始定位
    NSLog(@"开始定位");
    [_manager startUpdatingLocation];
}


#pragma mark - 代理方法  在每一次位置更新的时候调用
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //CLLocation 表示位置
    CLLocation *location = manager.location;
    
    //获取经纬度(结构体)
    CLLocationCoordinate2D coordinate =  location.coordinate;
    NSLog(@"经度 =%f 纬度 =%f",coordinate.longitude, coordinate.latitude);
    
}


//获取用户位置数据失败的回调方法,在此通知用户


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    if ([error code] == kCLErrorDenied)
    {
        NSLog(@"访问被拒绝");
    }
    if ([error code] == kCLErrorLocationUnknown) {
        NSLog(@"无法获取位置信息");
    }
}

相关文章

  • iOS-定位功能

  • iOS常用的小功能:扫描和生成二维码,涂鸦,得到一些信息设备

    不多说了,附上链接 https://github.com/helloLiYong/iOS- 常用小功能

  • iOS-定位

    使用地图的CLLocationManager完成定位效果.例子:定位当前所在位置,把所在城市的城市名显示在labe...

  • iOS-定位

    一、前言 二、定位权限 1、iOS8.* -前台定位 -需要在info.plist配置NSLocationWhen...

  • JFCitySelector轻量、灵活、可自定义的三级城市选择器

    简介 JFCitySelector最开始只是自己学习时写的demo(请看:iOS-(仿美团)城市选择器+自动定位+...

  • 定位

    定位 1.实现定位功能 在iOS中使用定位功能,需要导入CoreLocation.h文件,其实现定位功能的步骤如下...

  • 定位功能

    1.调用微信自带函数获取经纬度 2.引入js文件 3.将获取到的经纬度传入换取城市值

  • 定位功能

    1.App->info中添加请求定位说明。Privacy - Location Always Usage Desc...

  • 定位功能

    01-iOSCoreLocation框架的基本使用——定位 基本大纲 01-CoreLocation框架的基本使用...

  • 定位功能

    有用的excel定位功能 今天是2019年1月14号。老师在课件视频上教给我们的是exceL定位功能,和选择性...

网友评论

      本文标题:iOS-定位功能

      本文链接:https://www.haomeiwen.com/subject/stdosttx.html