ios地图解析

1.预览效果图

ios地图解析_第1张图片
Untitled1.gif

2.注意点

需导入头文件
#import //定位

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 * _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 * _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];
    }
    
        
        
        
   
}

4.还有欠妥,请多多指教。

你可能感兴趣的:(ios地图解析)