1.导入系统框架

/**
 *  界面效果1 实现定位到输入的地址,并且提示出地址的经纬度
 */

2.viewcontroller.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) CLGeocoder *geoCoder;
@end

3.viewcontroller.m

@interface ViewController ()<UISearchBarDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.geoCoder = [[CLGeocoder alloc]init];
    self.mapView.zoomEnabled = YES;
    self.mapView.rotateEnabled = NO;
    self.mapView.scrollEnabled = YES;
    self.mapView.showsUserLocation = YES;
    self.mapView.mapType = MKMapTypeStandard;
    self.searchBar.text = @"天安门";
    self.searchBar.delegate = self;
}
//点击搜索
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    //调用searchBar方法进行搜索
    [self startSearch:searchBar];
}
//点击取消
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    //调用searchBar方法进行搜索
    [self startSearch:searchBar];

}
//执行搜索
-(void)startSearch:(UISearchBar *)searchBar
{
    //关闭searchBar的虚拟键盘
    [self.searchBar resignFirstResponder];
    NSString *searchText = self.searchBar.text;
    if (searchText != nil && searchText.length>0) {
        [self locateAt:searchText];
    }
}
//输入文本时
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    //显示取消按钮
    searchBar.showsCancelButton = YES;
    //通过遍历找到取消按钮,并将取消按钮的文本设为搜索
    for (id cc in [searchBar.subviews[0]subviews]) {
        if ([cc isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)cc;
            [btn setTitle:@"搜索" forState:UIControlStateNormal];
        }
    }
}
//将字符串地址转换为经纬度,并执行定位
-(void)locateAt:(NSString *)add
{
   
[self.geoCoder geocodeAddressString:add
completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks,
NSError * _Nullable error) {
        if ([placemarks count]>0 && error == nil) {
            NSLog(@"搜索到匹配%lu条地址数据",(unsigned long)placemarks.count);
            //处理第一个地址
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            NSLog(@"经度 = :%f",placemark.location.coordinate.longitude);
            NSLog(@"纬度 = :%f",placemark.location.coordinate.latitude);
            NSLog(@"国家 = %@",placemark.country);
            NSLog(@"邮编 = %@",placemark.postalCode);
            NSLog(@"位置 = %@",placemark.locality);
           
NSString *msg = [NSString stringWithFormat:@"经度 = %f,纬度 =
%f",placemark.location.coordinate.longitude,placemark.location.coordinate.latitude];
           
[[[UIAlertView alloc]initWithTitle:@"信息" message:msg delegate:self
cancelButtonTitle:@"确定" otherButtonTitles:  nil]show];
            //设置地图显示的范围
            MKCoordinateSpan span;
            //地图显示范围越小,细节越清楚
            span.latitudeDelta = 0.0001;
            span.longitudeDelta = 0.0001;
            MKCoordinateRegion region = {placemark.location.coordinate,span};
            //设置地图中心位置为搜索到的位置
            [self.mapView setRegion:region];
            //创建一个,该对象将作为地图描点
            MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
            //设置地图锚点的坐标
            point.coordinate = placemark.location.coordinate;
            //设置地图锚点的标题
            point.title = placemark.name;
            //设置地图锚点的副标题
           
point.subtitle = [NSString
stringWithFormat:@"%@-%@-%@-%@",placemark.country,placemark.administrativeArea,placemark.locality,placemark.subLocality
];
            //将地图锚点添加到地图上
            [self.mapView addAnnotation:point];
            //选中指定锚点
            [self.mapView selectAnnotation:point animated:YES];
        }
        else
        {
            NSLog(@"无搜索结果");
        }
    }];
}

@end

/**
 *  界面效果2 点击界面1的解析按钮跳转到界面2

*         实现输入地址,解析到经纬度

*         输入经纬度,显示详细地址
 */

4.界面2的.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface jieXiViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *dizhijiexi;
@property (strong, nonatomic) IBOutlet UITextField *jingdu;
@property (strong, nonatomic) IBOutlet UITextField *weidu;
@property (strong, nonatomic) IBOutlet UITextView *showTV;
- (IBAction)jiexi:(id)sender;
- (IBAction)fanjeixi:(id)sender;
@property (strong, nonatomic) CLGeocoder* geocoder;
@end
5.界面2的.m

@interface jieXiViewController ()

@end

@implementation jieXiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建地址解析器
    self.geocoder = [[CLGeocoder alloc]init];
}

- (IBAction)jiexi:(id)sender {
    // 获取用户输入的地址字符串
    NSString *inputAdd = self.dizhijiexi.text;
    if (inputAdd != nil && inputAdd.length>0) {
       
[self.geocoder geocodeAddressString:inputAdd
completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks,
NSError * _Nullable error) {
            // 如果解析结果的集合元素的个数大于1,表明解析得到了经度、纬度信息
            if (placemarks.count > 0)
            {
                // 只处理第一个解析结果,实际项目中可使用列表让用户选择
                CLPlacemark* placemark = placemarks[0];
                CLLocation* location = placemark.location;
                self.showTV.text = [NSString stringWithFormat:
                                        @"%@的经度为:%g,纬度为:%g" , inputAdd ,
                                        location.coordinate.longitude ,
                                        location.coordinate.latitude ];
            }
            // 没有得到解析结果。
            else
            {
                // 使用UIAlertView提醒用户
                [[[UIAlertView alloc] initWithTitle:@"提醒"
                                            message:@"您输入的地址无法解析" delegate:nil
                                  cancelButtonTitle:@"确定" otherButtonTitles: nil]
                 show];
            }
 
        }];
    }
}

- (IBAction)fanjeixi:(id)sender {
    
    NSString* longitudeStr = self.jingdu.text;
    NSString* latitudeStr = self.weidu.text;
    if(longitudeStr != nil && longitudeStr.length > 0
       && latitudeStr != nil && latitudeStr.length > 0)
    {
        // 将用户输入的经度、纬度封装成CLLocation对象
        CLLocation* location = [[CLLocation alloc]
                                initWithLatitude:[latitudeStr floatValue]
                                longitude:[longitudeStr floatValue]];
        [self.geocoder reverseGeocodeLocation:location completionHandler:
         ^(NSArray *placemarks, NSError *error)
         {
             // 如果解析结果的集合元素的个数大于1,表明解析得到了经度、纬度信息
             if (placemarks.count > 0)
             {
                 // 只处理第一个解析结果,实际项目可使用列表让用户选择
                 CLPlacemark* placemark = placemarks[0];
                 // 获取详细地址信息
                 NSArray* addrArray = [placemark.addressDictionary
                                       objectForKey:@"FormattedAddressLines"];
                 // 将详细地址拼接成一个字符串
                 NSMutableString* addr = [[NSMutableString alloc] init];
                 for(int i = 0 ; i < addrArray.count ; i ++)
                 {
                     [addr appendString:addrArray[i]];
                 }
                 self.showTV.text = [NSString stringWithFormat:
                                         @"经度:%g,纬度:%g的地址为:%@" ,
                                         location.coordinate.longitude ,
                                         location.coordinate.latitude , addr];
             }
             // 没有得到解析结果。
             else
             {
                 // 使用UIAlertView提醒用户
                 [[[UIAlertView alloc] initWithTitle:@"提醒"
                                             message:@"您输入的地址无法解析" delegate:nil
                                   cancelButtonTitle:@"确定" otherButtonTitles: nil]
                  show];
             }
         }];
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
@end

地图:CLGeocoder地址解析与反地址解析的更多相关文章

  1. 【百度地图API】如何进行地址解析与反地址解析?——模糊地址能搜索到精确地理信息!

    原文:[百度地图API]如何进行地址解析与反地址解析?--模糊地址能搜索到精确地理信息! 摘要: 什么是地址解析? 什么是反地址解析? 如何运用地址解析,和反地址解析? 可以同时运用地址解析,和反地址 ...

  2. 【百度地图API】批量地址解析与批量反地址解析(带商圈数据)

    原文:[百度地图API]批量地址解析与批量反地址解析(带商圈数据) 摘要:因为地址解析的webserives方式还没有开通,所以先用JS版本的地址解析接口来批量获取地址解析数据吧,同时还能得到商圈的数 ...

  3. 【百度地图API】如何区分地址解析和智能搜索?

    原文:[百度地图API]如何区分地址解析和智能搜索? 摘要: 很多用户一直无法区分地址解析geocoder和智能搜索localsearch的使用场景.该文章用一个详尽的示例,充分展示了这两个类,共5种 ...

  4. 【高德地图API】从零开始学高德JS API(八)——地址解析与逆地址解析

    原文:[高德地图API]从零开始学高德JS API(八)——地址解析与逆地址解析 摘要:无论是百度LBS开放平台,还是高德LBS开放平台,其调用量最高的接口,必然是定位,其次就是地址解析了,又称为地理 ...

  5. 谷歌地图地理解析和反解析geocode.geocoder详解

    地址解析就是将地址(如:贵州省贵阳市)转换为地理坐标(如经度:106.71,纬度:26.57)的过程. 地理反解析和上面的过程相反是将地理坐标(如纬度:26.57,经度:106.71)转换为地址(中国 ...

  6. 谷歌地图地理解析和反解析geocode.geocoder详解(转)

    谷歌地图地理解析和反解析geocode.geocoder详解 谷歌Geocoder服务 实例代码 地址解析就是将地址(如:贵州省贵阳市)转换为地理坐标(如经度:106.71,纬度:26.57)的过程. ...

  7. angularjs-googleMap googleMap api地址解析与反解析

    1.js:根据地址得到经纬度var myplace=$scope.place;//获取输入的地址var geocoder = new google.maps.Geocoder();//创建geocod ...

  8. iOS 原生地图地理编码与反地理编码

    当我们要在App实现功能:输入地名,编码为经纬度,实现导航功能. 那么,我需要用到原生地图中的地理编码功能,而在Core Location中主要包含了定位.地理编码(包括反编码)功能. 在文件中导入 ...

  9. iOS地图 -- 地理编码和反地理编码

    地理编码和反地理编码 用到的类和方法 CLGeocoder --> 地理编码管理器 - (void)geocodeAddressString:(NSString *)addressString ...

随机推荐

  1. Python 线程(threading) 进程(multiprocessing)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  2. Log4net 列

    配置 <configuration> <configSections> <section name="log4net" type="Syst ...

  3. linux环境下编码的问题

    查看linux支持的编码格式: locale -a查看文件的编码格式: :set fileencodinglinux下文本编码转换: iconv -f gbk -t utf8 main.cpp > ...

  4. php常用mysql函数

    mysql_affected_rows: 得到 MySQL 最后操作影响的列数目. mysql_close: 关闭 MySQL 伺服器连线. mysql_connect: 开启 MySQL 伺服器连线 ...

  5. 基于VMware的eCos环境编译redboot(脚本配置redboot)

    基于VMware的ecos,redboot及hello world(1)安装请参照[[ecos学习2]wmware运行redboot[方法二]--图形实现配置 ] (2)修改内存布局文件:~/i386 ...

  6. shell之rm -rf的别名设置

    vim ~/.bashrc alias rm='read -p "Are you ready?" y && [ $y == "y" ] & ...

  7. tuple只有一个元素的时候,必须要加逗号

    In [1]: a = (1) In [2]: a Out[2]: 1 In [3]: a = (1,) In [4]: a Out[4]: (1,) 这是因为括号()既可以表示tuple,又可以表示 ...

  8. HDU ACM 题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

  9. ajax请求解析springmvc返回的json数据

    需要使用的框架 spring3.0 jquery1.9.0(简化ajax开发的js库) Jackson(json处理器):jackson-core-asl-1.9.2.jar,jackson-mapp ...

  10. jQuery插件之artDialog

    artDialog是一个非常强大的弹出框插件.默认有两个版本,一个是jQuery版,一个是javascript版.功能非常多,而且使用非常简单.不写了,直接贴上官网的预览运行地址,以后用得着的时候去那 ...