您的位置:首页 > 其它

地理编码反地理编码, 根据地名获取经纬度等信息

2016-07-12 17:43 471 查看
CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写。
1.使用CLGeocoder可以完成“地理编码”和“反地理编码”
地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
反地理编码:根据给定的经纬度,获得具体的位置信息
 
(1)地理编码方法
  - (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler; 
(2)反地理编码方法
  - (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
 
2.CLGeocodeCompletionHandler
  当地理\反地理编码完成时,就会调用CLGeocodeCompletionHandler
  


这个block传递2个参数
error :当编码出错时(比如编码不出具体的信息)有值
placemarks :里面装着CLPlacemark对象
 
3.CLPlacemark
说明:CLPlacemark的字面意思是地标,封装详细的地址位置信息
地理位置     @property (nonatomic,strong) CLLocation *location;  
区域       @property (nonatomic,strong) CLRegion *region;
详细的地址信息   @property (nonatomic, strong) NSDictionary *addressDictionary;
地址名称    @property (nonatomic, copy) NSString *name;
城市      @property (nonatomic,copy) NSString *locality;

#import
<CoreLocation/CoreLocation.h>

@property(nonatomic,strong)CLGeocoder
* geocoder;

@property(nonatomic,strong)CLLocation
* location1;

#pragma mark
根据地名确定地理坐标
-(void)getCoordinateByAddress:(NSString
*)address{

   
//地理编码
    [_geocodergeocodeAddressString:addresscompletionHandler:^(NSArray
*placemarks,NSError *error) {

       //取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址
       
CLPlacemark *placemark=[placemarksfirstObject];

        
       
self.location1=placemark.location;//位置
       
CLRegion *region=placemark.region;//区域
       
NSDictionary *addressDic= placemark.addressDictionary;//详细地址信息字典,包含以下部分信息
       
NSLog(@"%@",self.location1); 
     
    }];  
     }

#pragma mark 根据地理坐标确定地名

-(void)getAddressByCoordinate:(NSString *)latitudeText  with:(NSString *)longtitudeText{

  CLLocationDegrees latitude=[latitudeTextdoubleValue];
   
CLLocationDegrees longitude=[longtitudeTextdoubleValue];

    
   
CLLocation *location=[[CLLocationalloc]initWithLatitude:latitudelongitude:longitude];

   
//2.反地理编码
    [self.geocoderreverseGeocodeLocation:locationcompletionHandler:^(NSArray
*placemarks,NSError *error) {
        
if (error||placemarks.count==0) {

            
self.reverdeDetailAddressLabel.text=@"你输入的地址没找到";
         }else//编码成功
         {
            
//显示最前面的地标信息
            
CLPlacemark *firstPlacemark=[placemarksfirstObject];
            
self.reverdeDetailAddressLabel.text=firstPlacemark.name;
           
//经纬度

            
CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;

           
CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
            
self.latitudeField.text=[NSStringstringWithFormat:@"%.2f",latitude];
            
self.longitudeField.text=[NSStringstringWithFormat:@"%.2f",longitude];
        }
}];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: