地理編碼與反地理編碼

  • 使用CLGeocoder可以完成“地理編碼”和“反地理編碼”
  • 地理編碼:根據給定的地名,獲得具體的位置信息(比如經度和緯度,以及地址的全稱)
  • 反地理編碼:根據給定的經度和緯度,獲取具體的位置信息

(一)地理編碼

  • 具體的做法:(就兩步)

    1.創建地理編碼對象:導入框架#import <CoreLocation/CoreLocation.h>
    
         CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    
    2.利用地理編碼對象編碼:聲明一個@property(nonatomic,strong) CLGeocoder *geocoder;屬性
    

    調用一個地理編碼方法:(很重要)

    [self.geocoder geocodeAddressString:self.addressFiled.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error)
    

地理編碼方法:


編碼展示

下面是具體的代碼:(在storyBoard里面畫的,1個button,1個UITextFiled,3個UILabel)

 #import "ViewController.h"
 #import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>

/*
   需要編碼的地址容器
*/
@property (weak, nonatomic) IBOutlet UITextField *addressFiled;

/*
    經度容器
*/
 @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
/*
    緯度容器
*/
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
/*
    詳情容器
*/
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;
/*
    監聽地理編碼點擊事件
*/

- (IBAction)geocoderBtnClick;

@property(nonatomic,strong) CLGeocoder *geocoder;

@end

@implementation ViewController

//1.創建地理編碼對象
-(CLGeocoder *)geocoder
{
  if (!_geocoder) {
    
    _geocoder = [[CLGeocoder alloc]init];
}

  return _geocoder;
}

- (void)viewDidLoad {
[super viewDidLoad];

self.addressFiled.clearButtonMode = UITextFieldViewModeAlways;
self.addressFiled.placeholder = @"    請輸入地理位置";

}


- (IBAction)geocoderBtnClick {

//獲取用戶輸入的位置
NSString *addressString = self.addressFiled.text;

NSLog(@"%@",addressString);

if (addressString == nil || addressString.length == 0) {
    
    NSLog(@"請輸入地址");
    return;
}

//2.利用地理編碼對象編碼
//根據傳入的地址獲取該地址的對應的經緯度信息
[self.geocoder geocodeAddressString:self.addressFiled.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    
    //placemarks 地標  地標數組里面存放著地標,每一個地標包含了該位置的經緯度,以及城市/區域/國家代碼/郵編等等....
    if (placemarks.count == 0 || error != nil) {
        
        return;
    }

    //獲取數組里面第一個信息
    CLPlacemark *placemark = [placemarks firstObject];
    
    self.latitudeLabel.text = [NSString stringWithFormat:@"   %f", placemark.location.coordinate.latitude];
    
    self.longitudeLabel.text = [NSString stringWithFormat:@"   %f",placemark.location.coordinate.longitude];
    
    NSArray *array = placemark.addressDictionary[@"FormattedAddressLines"];
    
    NSMutableString *stringAddstring = [NSMutableString string];
    
    for (NSString *string in array) {
        [stringAddstring appendString:string];
    }
    
    self.detailAddressLabel.text = stringAddstring;
    
    NSLog(@"%@ %@ %f %f",placemark.name,placemark.addressDictionary,placemark.location.coordinate.latitude,placemark.location.coordinate.longitude);
    
}];

 }
 @end

(二)反地理編碼

    1.創建地理編碼對象:導入框架#import <CoreLocation/CoreLocation.h>

       CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    2.利用地理編碼對象編碼:聲明一個@property(nonatomic,strong) CLGeocoder *geocoder;屬性

調用一個反地理編碼方法:(很重要)

[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) 
反編碼
  • 下面是具體的代碼

    #import "ViewController.h"
    #import <CoreLocation/CoreLocation.h>
    @interface ViewController ()<CLLocationManagerDelegate>
    
    - (IBAction)clickButton;
    
    @property (weak, nonatomic) IBOutlet UITextField *latitudeValue;
    
    @property (weak, nonatomic) IBOutlet UITextField *longitudeValue;
    
    @property (weak, nonatomic) IBOutlet UILabel *labelText;
    
    @property(nonatomic,strong) CLGeocoder *geocoder;
    
    @end
    
    @implementation ViewController
    
    -(CLGeocoder *)geocoder
    {
       if (!_geocoder) {
      
         _geocoder = [[CLGeocoder alloc]init];
        
     }
    
       return _geocoder;
    }
    
     - (void)viewDidLoad {
     [super viewDidLoad];
    
     }
    
    - (IBAction)clickButton {
    
      //1.獲取用戶輸入的經緯度
    
     NSString *latitudeString = self.latitudeValue.text;
    
     NSString *longitudeValueString = self.longitudeValue.text;
    
     if (latitudeString == nil || latitudeString.length == 0 || longitudeValueString == nil || longitudeValueString.length == 0) {
      
      NSLog(@"請輸入經緯度");
      
      return;
     }
    
     //2.根據用戶輸入的經緯度創建CLLocation對象
    
      CLLocation *location = [[CLLocation alloc]initWithLatitude:[latitudeString doubleValue] longitude:[longitudeValueString doubleValue]];
    
     //3.根據CLLocation對象獲取相應的地表信息
    
      [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    
      for (CLPlacemark *placemark in placemarks) {
          
          self.labelText.text = placemark.locality;
          NSLog(@"哈哈");
          
        }
      
     }];
    
    }
    @end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容