Geocoder
地理編碼 : 人文信息轉化為地理信息的過程
反地理編碼 : 地理信息轉換為人文信息的過程
地理編碼步驟:
1.創建地理編碼者 (CLGeocoder)
2.地理編碼 geocodeAddress...反地理編碼步驟:
1.創建地理編碼者(CLGeocoder)
2.地理信息轉位置對象
3.反地理編碼取值 reverseGeocodeLocation地理編碼 簡單界面搭建:
geocoder.png
#import "GeocoderViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface GeocoderViewController ()
// 地址輸入框
@property (weak, nonatomic) IBOutlet UITextField *address_TF;
// 緯度文本
@property (weak, nonatomic) IBOutlet UILabel *latitude_Label;
// 經度文本
@property (weak, nonatomic) IBOutlet UILabel *longitude_Label;
// 詳情文本
@property (weak, nonatomic) IBOutlet UILabel *detail_Label;
@end
@implementation GeocoderViewController
- (IBAction)geocoderBtnClick:(id)sender {
// 創建地理編碼者
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
// 地理編碼
[geocoder geocodeAddressString:self.address_TF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// (泛型) NSArray<CLPlacemark *> * _Nullable placemarks 存放地標對象的數組 _Nullable可能為空,所以需要做非空判斷
if ( placemarks.count == 0 || error) {
NSLog(@"查詢失敗: %@",error);
return ;
}
// 可能有重名,所以結果是一個數組
CLPlacemark *place = placemarks.lastObject;
// 設置數據
self.latitude_Label.text = [NSString stringWithFormat:@"%f",place.location.coordinate.latitude];
self.longitude_Label.text = [NSString stringWithFormat:@"%f",place.location.coordinate.longitude];
self.detail_Label.text = [NSString stringWithFormat:@"%@",place.name];
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
演示結果:
geocoder_result.png
- 反地理編碼 簡單界面搭建:
reverseGeoCoder.png
#import "ReverseGeocoderViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ReverseGeocoderViewController ()
@property (weak, nonatomic) IBOutlet UITextField *latitude_TF;
@property (weak, nonatomic) IBOutlet UITextField *longitude_TF;
@property (weak, nonatomic) IBOutlet UILabel *city_Label;
@end
@implementation ReverseGeocoderViewController
- (IBAction)reverseGeoCoderBtnClick:(id)sender {
// 創建地理編碼者
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
// 獲取位置信息對應的位置對象
CLLocation *location = [[CLLocation alloc]initWithLatitude:[self.latitude_TF.text floatValue] longitude:[self.longitude_TF.text floatValue]];
// 轉換人文信息
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// 非空處理
if (placemarks.count == 0 || error) {
NSLog(@"查詢失敗");
return ;
}
CLPlacemark *place = placemarks.lastObject;
self.city_Label.text = place.locality;
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
演示結果:
reverseGeoCoder_result.png
CLPlacemark 地標對象屬性介紹:
// 位置對象,包含地理信息
@property (nonatomic, readonly, copy, nullable) CLLocation *location;
// 區域,范圍
@property (nonatomic, readonly, copy, nullable) CLRegion *region;
// 時區
@property (nonatomic, readonly, copy, nullable) NSTimeZone *timeZone NS_AVAILABLE(10_11,9_0);
// 地址字典 以字典的形式包含地址(人文信息)
@property (nonatomic, readonly, copy, nullable) NSDictionary *addressDictionary;
// address dictionary properties
@property (nonatomic, readonly, copy, nullable) NSString *name; // 名稱
@property (nonatomic, readonly, copy, nullable) NSString *thoroughfare; // 街道名
@property (nonatomic, readonly, copy, nullable) NSString *subThoroughfare; // 門牌號
@property (nonatomic, readonly, copy, nullable) NSString *locality; // 城市
@property (nonatomic, readonly, copy, nullable) NSString *subLocality; // 區
@property (nonatomic, readonly, copy, nullable) NSString *administrativeArea; // 州,省
@property (nonatomic, readonly, copy, nullable) NSString *subAdministrativeArea; // 郡,縣
@property (nonatomic, readonly, copy, nullable) NSString *postalCode; // 郵政編碼
@property (nonatomic, readonly, copy, nullable) NSString *ISOcountryCode; // ISO國家代號
@property (nonatomic, readonly, copy, nullable) NSString *country; // 國家
@property (nonatomic, readonly, copy, nullable) NSString *inlandWater; // 內河
@property (nonatomic, readonly, copy, nullable) NSString *ocean; // 海洋
@property (nonatomic, readonly, copy, nullable) NSArray<NSString *> *areasOfInterest; // 感興趣的點