ios地图解析

1.预览效果图

Untitled1.gif

2.注意点

需导入头文件
#import <CoreLocation/CoreLocation.h>//定位

3.代码展示

//地理编码
@property(nonatomic,strong) CLGeocoder *geocoder;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建地址解析器
    self.geocoder = [[CLGeocoder alloc]init];
    
}
//正向解析
- (IBAction)jiexi:(id)sender {
    //获取输入地址信息
    NSString *addre = self.address.text;
    if (self.address.text != nil && addre.length > 0) {
        
        [self.geocoder geocodeAddressString:addre completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
           //placemarks 只有大于 0 的时候才能表明得到的是经度和纬度
            if (placemarks.count > 0) {
                //取第一条信息
                CLPlacemark *placemark = placemarks[0];
                //经纬度
                CLLocation *location = placemark.location;
               
                
                self.textView.text = [NSString stringWithFormat:@"%@经度为:%g维度为:%g",addre,location.coordinate.longitude,location.coordinate.latitude];
            }
        }];
    }else
    {
        UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"您输入的地址无法解析" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alerView show];
    }
    
}
//反向解析
- (IBAction)fanjiexi:(id)sender {
    //得到经纬度内容
    NSString *longgitudeStr = self.jingdu.text;
    NSString *latitudeStr = self.weidu.text;
    if (longgitudeStr.length > 0 && latitudeStr.length > 0 && longgitudeStr!= nil && latitudeStr != nil) {
        //将用户输入的经纬度封装成CLLocation对象
        CLLocation *location = [[CLLocation alloc]initWithLatitude:[latitudeStr floatValue] longitude:[longgitudeStr floatValue]];
        
        [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            
            //如果解析结果的集合元素大于1,表明解析得到了经度,维度信息
            if (placemarks.count > 0) {
                CLPlacemark *placmark = placemarks[0];
                NSLog(@"===%@",placmark.addressDictionary);
                //获取详细地址信息
                NSArray *addArr = [placmark.addressDictionary objectForKey:@"FormattedAddressLines"];
                //将详细地址拼接成一个字符串
                NSMutableString *addr = [[NSMutableString alloc]init];
                for (int i = 0; i < addArr.count; i ++) {
                    [addr appendString:addArr[i]];
                }
                self.textView.text = [NSString stringWithFormat:@"维度:%g经度:%g的地址为:%@",location.coordinate.latitude,location.coordinate.longitude,addr];
            }
            
        }];
        
        
    }else
    {
        UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"您输入的地址无法解析" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alerView show];
    }
    
        
        
        
   
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,676评论 25 708
  • 最近学习iOS地图比较多,所以做笔记记录一下,作为总结 对于地图和定位,苹果公司提供给了两个框架:MapKit:用...
    往日的时光阅读 1,615评论 0 1
  • 场景:一个页面如果通过ajax请求加载数据时,如果跳转到下一页面再回来,这时候数据会消失,需要再次通过ajax请求...
    幽暗金阅读 9,544评论 0 1
  • 今天他又去楼下打牌了,到2点40回来。我从十二点过后给他发了三次短信。说了一些不满的话,每次回家他都很晚才来睡,我...
    aseeya阅读 246评论 0 0
  • 有句话说:人的一生只能做好一件事。是因为人的精力是有限的;不可能什么都去做,去尝试。年轻时,我们对什么都好奇;喜欢...
    Miss_L甜品师阅读 2,551评论 2 3

友情链接更多精彩内容