• iOS根据经纬度获得地理名称


    1、首先加入工程的静态库

      Build Phases -> Link Binary With Libraries -> +

      

    2、在需要的VC中头文件、添加代理

      

    3、在.m中声明属性

      

    4、初始化CLLocationManager

      

    5、CLLocationManagerDelegate - 方法

      - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

      {

        //将经度显示到label

        self.longitude.text = [NSString stringWithFormat:@"%lf", newLocation.coordinate.longitude];

          //将纬度现实到label

        self.latitude.text = [NSString stringWithFormat:@"%lf", newLocation.coordinate.latitude];

          // 获取当前所在的城市名

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

          //根据经纬度反向地理编译出地址信息

          [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error){

                if (array.count > 0){

                    CLPlacemark *placemark = [array objectAtIndex:0];

                    //将获得的所有信息显示到label

        //            self.location.text = placemark.name;

        //            self.location.text = placemark.country;//中国

        //            self.location.text = placemark.administrativeArea;//山西省

        //            self.location.text = placemark.locality;//太原市

        //            self.location.text = placemark.subLocality;//万柏林区

        //            self.location.text = [NSString stringWithFormat:@"%@%@%@",placemark.administrativeArea,placemark.location,placemark.subLocality];

                    NSString *administrativeAreaStr = placemark.administrativeArea;

                    NSString *localityStr = placemark.locality;

                    NSString *subLocalityStr = placemark.subLocality;

                    self.location.text = [NSString stringWithFormat:@"%@ %@ %@",administrativeAreaStr,localityStr,subLocalityStr];

                    NSLog(@"信息1:%@",placemark.name);

                    //获取城市

                    NSString *city = placemark.locality;

                      if (!city) {

                            //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)

                            city = placemark.administrativeArea;

                      }

                    NSLog(@"city = %@", city);

                    _cityLable.text = city;  

      //            [_cityButton setTitle:city forState:UIControlStateNormal];

              }

              else if (error == nil && [array count] == 0)

              {

                  NSLog(@"No results were returned.");

              }

              else if (error != nil)

              {

                  NSLog(@"An error occurred = %@", error);

              }

          }];

          //系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新

          [manager stopUpdatingLocation];

      }

    6、在Info.plist文件中加入

        NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription 设置为String Valu是提示信息可以为空。

    7、运行结果

                    

    附加:一个可以不用于真机调试而测试定位信息

      

  • 相关阅读:
    Intent属性详解
    LIBGDX游戏引擎平台介绍与搭建
    android教程之intent对象
    android教程之日期时间控件DatePicker/TimePicker
    DotNet Core 3.1 EF Core 数据库迁移(Migration)
    微服务介绍
    Asp.Net Core 认证授权:Cookie-based
    IdentityServer4 实现自定义 GrantType 授权模式
    SqlServer配置主从复制
    在【Stimulsoft-Reports-Net-2016.1】中使用DataSet做数据源新建报表
  • 原文地址:https://www.cnblogs.com/ChouDanDan/p/5230079.html
Copyright © 2020-2023  润新知