zoukankan      html  css  js  c++  java
  • 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/
  • 相关阅读:
    hdu 4622 Reincarnation 字符串hash 模板题
    NYOJ 994 海盗分金 逆向递推
    hdu 4679 Terrorist’s destroy 树形DP
    Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化
    hdu 5535 Cake 构造+记忆化搜索
    poj 3415 Common Substrings 后缀数组+单调栈
    poj 3518 Corporate Identity 后缀数组->多字符串最长相同连续子串
    poj 2774 Long Long Message 后缀数组LCP理解
    hdu 3518 Boring counting 后缀数组LCP
    poj 3641 Pseudoprime numbers Miller_Rabin测素裸题
  • 原文地址:https://www.cnblogs.com/xvewuzhijing/p/4897843.html
Copyright © 2011-2022 走看看