zoukankan      html  css  js  c++  java
  • iOS之获取经纬度并通过反向地理编码获取详细地址

     1 _locationManager = [[CLLocationManager alloc] init];
     2 
     3     //期望的经度
     4 
     5     _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
     6 
     7     //大约变化100米更新一次
     8 
     9     _locationManager.distanceFilter = 100;
    10 
    11     //认证NSLocationAlwaysUsageDescription
    12 
    13     if ([[UIDevice currentDevice] systemVersion].doubleValue > 8.0) {//如果iOS是8.0以上版本
    14 
    15         if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {//位置管理对象中有requestAlwaysAuthorization这个方法
    16 
    17             //运行
    18 
    19             [_locationManager requestAlwaysAuthorization];
    20 
    21         }
    22 
    23     }
    24 
    25     _locationManager.delegate = self;
    26 
    27     [_locationManager startUpdatingLocation];
    28 
    29  
    30 
    31 //获取经纬度和详细地址
    32 
    33 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    34 
    35     
    36 
    37     CLLocation *location = [locations lastObject];
    38 
    39     NSLog(@"latitude === %g  longitude === %g",location.coordinate.latitude, location.coordinate.longitude);
    40 
    41     
    42 
    43     //反向地理编码
    44 
    45     CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
    46 
    47     CLLocation *cl = [[CLLocation alloc] initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
    48 
    49     [clGeoCoder reverseGeocodeLocation:cl completionHandler: ^(NSArray *placemarks,NSError *error) {
    50 
    51         for (CLPlacemark *placeMark in placemarks) {
    52 
    53             
    54 
    55             NSDictionary *addressDic = placeMark.addressDictionary;
    56 
    57             
    58 
    59             NSString *state=[addressDic objectForKey:@"State"];
    60 
    61             NSString *city=[addressDic objectForKey:@"City"];
    62 
    63             NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
    64 
    65             NSString *street=[addressDic objectForKey:@"Street"];
    66 
    67             
    68 
    69             NSLog(@"所在城市====%@ %@ %@ %@", state, city, subLocality, street);
    70 
    71             [_locationManager stopUpdatingLocation];
    72 
    73         }
    74 
    75     }];
    76 
    77 }
    78 
    79  
  • 相关阅读:
    Alpha 冲刺 (8/10)
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
    团 队 作 业 ———— 随 堂 小 测
    Alpha 冲刺 (5/10)
    Alpha 冲刺 (4/10)
    Beta冲刺博客汇总(麻瓜制造者)
    Beta冲刺(3/5)(麻瓜制造者)
    快速搭建一个Express工程骨架
    个人作业——软件产品案例分析
  • 原文地址:https://www.cnblogs.com/feiyiban588/p/5596477.html
Copyright © 2011-2022 走看看