地图:CLGeocoder地址解析与反地址解析

地图:CLGeocoder地址解析与反地址解析1.导入系统框架/***界面效果1实现定位到输入的地址,并且提示出地址的经纬度*/2.viewcontroller.h#import#import@interfaceViewController:UIViewController@property(strong,nonatomic

大家好,欢迎来到IT知识分享网。

1.导入系统框架

地图:CLGeocoder地址解析与反地址解析

 

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

地图:CLGeocoder地址解析与反地址解析       地图:CLGeocoder地址解析与反地址解析

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;
            ;
        }
    }
}
//将字符串地址转换为经纬度,并执行定位
-(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

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

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

地图:CLGeocoder地址解析与反地址解析     地图:CLGeocoder地址解析与反地址解析

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

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/31182.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信