• iOS定位


    1.首先先导入CoreLocation.framework,在Build Phases ->Link Binary With Libraries

    2.创建一个单例,或直接在当前工程,导入头文件 #import <CoreLocation/CoreLocation.h>,并设置代理NSObject<CLLocationManagerDelegate>。

    3.注意事项

    //从iOS 6开始,苹果在保护用户隐私方面做了很大的加强,以下操作都必须经过用户批准授权:要想获得用户的位置,想访问用户的通讯录、日历、相机、相册等;当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权。
    
    //
    
    //可以在Info.plist中设置NSLocationUsageDescription说明定位的目的(Privacy - Location Usage Description)
    
    //
    
    //从iOS 8开始,用户定位分两种情况
    
    //总是使用用户位置:NSLocationAlwaysUsageDescription
    
    //使用应用时定位:NSLocationWhenInUseDescription
    
    //当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权
    
     
    
    //*打开定位服务
    
    //*需要在info.plist文件中添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):
    
    //*NSLocationWhenInUseUsageDescription 允许在前台使用时获取GPS的描述
    
    //*NSLocationAlwaysUsageDescription 允许永远可获取GPS的描述
    //地图分为,地理编码是通过地址得到经纬度,地理反编码是通过经纬度得到地址
    
    //开始定位

    4.代码如下

    //.h文件
    
    + (KLocation *)shareLocation;
    
    /**
     *  开启定位
     */
    - (void)startLocation;
    /**
     *  关闭定位
     */
    - (void)stopLocation;
    + (KLocation *)shareLocation{
        
        static KLocation *location = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            location = [[KLocation alloc] init];
        });
        return location;
    }
    - (instancetype)init{
        if (self = [super init]) {
            
            _entity = [KEntity shareEntity];
        }
        return self;
    }
    //1.iOS8中定位服务的变化:CLLocationManager协议方法不响应,无法回掉GPS方法,不出现获取权限提示,解决方法如下,
    //如果需要仅在前台定位,你在调用startUpdatingLocation 前需要调用requestWhenInUseAuthorization
    //
    //如果需要在前后台定位,你在调用startUpdatingLocation 前需要调用requestAlwaysAuthorization
    //
    //在plist文件中添加NSLocationWhenInUseUsageDescription或(与)NSLocationAlwaysUsageDescription字段:
    //找到info.plist文件->右击->Open As->Source Code->在尾部的</dict>标签之前添加以下一个或两个:
    //<key>NSLocationWhenInUseUsageDescription</key><string>需要定位</string>
    //<key>NSLocationAlwaysUsageDescription</key><string>需要定位</string>
    //2.用户隐私的保护
    //从iOS 6开始,苹果在保护用户隐私方面做了很大的加强,以下操作都必须经过用户批准授权:要想获得用户的位置,想访问用户的通讯录、日历、相机、相册等;当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权。
    //
    //可以在Info.plist中设置NSLocationUsageDescription说明定位的目的(Privacy - Location Usage Description)
    //
    //从iOS 8开始,用户定位分两种情况
    //总是使用用户位置:NSLocationAlwaysUsageDescription
    //使用应用时定位:NSLocationWhenInUseDescription
    //当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权
    
    //*打开定位服务
    //*需要在info.plist文件中添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):
    //*NSLocationWhenInUseUsageDescription 允许在前台使用时获取GPS的描述
    //*NSLocationAlwaysUsageDescription 允许永远可获取GPS的描述
    
    //地图分为,地理编码是通过地址得到经纬度,地理反编码是通过经纬度得到地址
    //开始定位
    - (void)startLocation{
        
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        
        //开启定位
        [_locationManager requestWhenInUseAuthorization];
    //    [_locationManager requestAlwaysAuthorization];
        if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            NSLog(@"requestWhenInUseAuthorization");
            [_locationManager requestWhenInUseAuthorization];
        }
        
        //开始定位,不断调用其代理方法
        [_locationManager startUpdatingLocation];
    }
    //停止定位
    - (void)stopLocation{
    
    }
    //通过定位,获取地址,反地理编码
    - (void)getAdressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{
        
        //初始化定位
        CLLocation *cllocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
        _geocoder = [[CLGeocoder alloc] init];
        
        
        
        //调用 geocoder 的block方法 reverseGeocodeLocation
        
        [_geocoder reverseGeocodeLocation:cllocation completionHandler:^(NSArray *placemarks, NSError *error) {
            
            CLPlacemark *placeMark = [placemarks lastObject];  //获取地址
            
            NSDictionary *dict = placeMark.addressDictionary;            //地址
            NSString *administrativeArea = placeMark.administrativeArea; //
            NSLog(@"City:%@",[dict objectForKey:@"City"]);
            NSLog(@"country%@",[dict objectForKey:@"Country"]);
            NSLog(@"FormattedAddressLines%@",[dict objectForKey:@"FormattedAddressLines"]);
            NSLog(@"name%@",[dict objectForKey:@"Name"]);
            NSLog(@"State%@",[dict objectForKey:@"State"]);
            NSLog(@"Street%@",[dict objectForKey:@"Street"]);
            NSLog(@"SubLocality%@",[dict objectForKey:@"SubLocality"]);
            NSLog(@"SubThoroughfare%@",[dict objectForKey:@"SubThoroughfare"]);
            NSLog(@"Thoroughfare%@",[dict  objectForKey:@"Thoroughfare"]);
            NSLog(@"administrativeArea%@",administrativeArea);
            
            _entity.latitude = [NSString stringWithFormat:@"%f",latitude];
            _entity.longitude = [NSString stringWithFormat:@"%f",longitude];
            _entity.city = [NSString stringWithFormat:@"%@",[dict objectForKey:@"City"]];
            _entity.district = [NSString stringWithFormat:@"%@",[dict objectForKey:@"SubLocality"]];
            _entity.province = administrativeArea;
        }];
        
        
    }
    //2016-04-11 18:10:35.980 HopeHelpServer[7518:2567331] City:北京市
    //331] name中国北京市西城区展览路街道桃柳园西巷16号A座
    //2016-04-11 18:10:35.980 HopeHelpServer[7518:2567331] State北京市
    //2016-04-11 18:10:35.980 HopeHelpServer[7518:2567331] Street桃柳园西巷16号A座
    //2016-04-11 18:10:35.980 HopeHelpServer[7518:2567331] SubLocality西城区
    //2016-04-11 18:10:35.981 HopeHelpServer[7518:2567331] SubThoroughfare16号A座
    //2016-04-11 18:10:35.981 HopeHelpServer[7518:2567331] Thoroughfare桃柳园西巷
    #pragma mark --- delegate
    
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
        
        //获取用户的位置
        _location = [locations lastObject];
        CLLocationCoordinate2D coordinate = _location.coordinate;
         NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude);
        
        //获取用户详细信息
        [self getAdressByLatitude:coordinate.latitude longitude:coordinate.longitude];
        // 2.停止定位
        [manager stopUpdatingLocation];
        
    }
    
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
        
    }
    将来的自己,会感谢现在不放弃的自己!
  • 相关阅读:
    fpga配置方式 .jic固化为ps模式
    fpga新建nios
    四轴飞行器飞行原理与双闭环PID控制
    fpga为什么要用nios 开发
    error A space is required after ',' comma-spacing
    vuex : Newline required at end of file but not found eol-last
    vue -Missing space before value for key 'path'vue.js解决空格报错
    visual studio 自动补全功能 以及代码没有颜色
    hadoop 伪分布模式环境搭建
    django框架-DRF工程之认证功能
  • 原文地址:https://www.cnblogs.com/TheYouth/p/5381454.html
Copyright © 2020-2023  润新知