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/
  • 相关阅读:
    Pwn-warmup_csaw_2016 writeup
    操作系统习题总结
    操作系统-存储器管理部分(待更新)
    树与二叉树之间的互相转换
    黑客攻防技术宝典-反病毒篇笔记(三)
    jaegeropentracing的Java-client完整分布式追踪链
    jaegeropentracing的Java-client
    IDEA2018.2版本注册
    Spring整合CXF webservice restful 实例
    带有WS-Security验证的webservice
  • 原文地址:https://www.cnblogs.com/xvewuzhijing/p/4897843.html
Copyright © 2011-2022 走看看