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. 是什么让我想到开发NFinal

    我是从01前开始就接触.net,那时.net还是1.0时代,很多东西都没有.后来.net出了2.0版本.从vs2005开始就使用Webform做网站.当时感觉.net能够拖来拖去,很厉害.参加工作后, ...

  2. vs2012 boost配置

    1.去www.boost.org下载最新的boost,我下载了boost_1_60_02.(我放在D:/cpp目录下)解压到当前文件夹3.打开VS2012->VS TOOLS->VS命令提 ...

  3. FFMPEG视音频解码【一】

    多媒体的时代,得多了解点编解码的技术才行,而ffmpeg为我们提供了一系列多媒体编解码的接口,如何用好这些接口达到自己所需要的目的,这也是一门重要的学问. 要是了解得不够,总是会遇到一堆又一堆问题:网 ...

  4. 关于char/varchar(n)中n的探究:字符数or字节数

    [问题来源]将设计的数据库表展示的时候,yu哥问我,你的那个top_info字段定义的类型是varchar(100),为什么是100呢,这100的长度能存多少个中文? 当时的想法就是,这个100能存多 ...

  5. 轻量级jquery框架之--树(tree)

    前言 在常用的UI组件中,树形组件与数据列表组件可以说是构成一个管理平台基本的两大数据核心组件.树形组件用于系统菜单,数据列表用于数据表现,两者配合即可完成一个简单的数据系统.要实现一个支持复选.工具 ...

  6. python-整理-logging日志

    python的日志功能模块是logging 功能和使用方式非常类似于log4 如何使用logging: # 导入日志模块import logging# 使用配置文件设置日志时,需要导入这个模块 imp ...

  7. [C++程序设计]返回指针值的函数

    定义指针函数的一般形式为 类型名 *函数名(参数表列); 例如 int *a(int x,int y);

  8. tp中u方法的使用

    自学的时候都没怎么使用过该方法,现在刚进入一个新公司参加项目.发现这个方法用的很多,所以记录下来防止以后忘了. U方法用于完成对URL地址的组装,特点在于可以自动根据当前的URL模式和设置生成对应的U ...

  9. Nightmare(BFS)

    #include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...

  10. query通用开源框架

    Jquery通用开源框架之[ejq.js] 简介 ejq是一款非常小巧的JS工具库,未压缩才50K,在jquery的基础上对jquery缺失部分作了很好的弥补作用. 优点: 1.具有内置的模板解析引擎 ...