• iOS开发>学无止境


    注:自iOS8起,系统定位功能进行了升级,SDK为了实现最新的适配,自v2.5.0起也做了相应的修改,开发者在使用定位功能之前,需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):

    NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述

    NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述

     _locationMag = [[CLLocationManager alloc] init];

        

        if (![CLLocationManager locationServicesEnabled]) {

            NSLog(@"定位服务当前可能尚未打开,请设置打开!");

            return;

        }

        //如果没有授权则请求用户授权

        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){

            [_locationMag requestWhenInUseAuthorization];

        }else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse){

            //设置代理

            _locationMag.delegate=self;

            //设置定位精度

            _locationMag.desiredAccuracy = kCLLocationAccuracyBest;

            //启动定位

            [_locationMag startUpdatingLocation];

        }

    #pragma mark - CoreLocation 代理

    #pragma mark 跟踪定位代理方法,每次位置发生变化即会执行(只要定位到相应位置)

    //可以通过模拟器设置一个虚拟位置,否则在模拟器中无法调用此方法

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

        

        CLLocation *location=[locations firstObject];//取出第一个位置

        CLLocationCoordinate2D coordinate = location.coordinate;//位置坐标

      NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);

        

        CLLocation *c = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];

        //创建位置

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

        [revGeo reverseGeocodeLocation:c completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

            

            if (!error && placemarks.count > 0) {

                NSDictionary *dict = [[placemarks objectAtIndex:0] addressDictionary];

                //  Country(国家)  State(城市)  SubLocality(区) Street(详细)

                NSLog(@"%@",[dict objectForKey:@"Country"]);

                NSLog(@"%@",[dict objectForKey:@"State"]);

                NSLog(@"%@",[dict objectForKey:@"SubLocality"]);

                NSLog(@"%@",[dict objectForKey:@"Street"]);

                NSString *stateStr = [dict objectForKey:@"State"];

                

                if (stateStr.length != 0) {

                    [backButton setTitle:[NSString stringWithFormat:@"%@",stateStr] forState:UIControlStateNormal];

                    

                    [MFCurrentCity shareInstanceCityName].cityNameString = stateStr;

                    [MFCurrentCity shareInstanceCityName].selectCityName = stateStr;

                    //                NSLog(@"cityLocationView is -- %@",cityLocationView.cityNameStr);

                    

                    [SVProgressHUD dismiss];

                } else {

                    [SVProgressHUD dismiss];

                    [self locationTap];

                }

            }

        }];

        //如果不需要实时定位,使用完即使关闭定位服务

        [_locationMag stopUpdatingLocation];

    }

    本内容来自: 超越昨天(学无止境) - http://www.cnblogs.com/xvewuzhijing/
  • 相关阅读:
    阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
    阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
    阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
    阶段3 2.Spring_03.Spring的 IOC 和 DI_1 ioc的概念和作用
    阶段3 2.Spring_03.Spring的 IOC 和 DI_13 课程知识梳理
    阶段3 2.Spring_03.Spring的 IOC 和 DI_12 注入集合数据
    dm-verity
    高通msm8909耳机调试
    NC和NO、耳机美标和欧标的区别
    动态绑定与静态绑定
  • 原文地址:https://www.cnblogs.com/xvewuzhijing/p/4897843.html
Copyright © 2020-2023  润新知