CLGeocoder 是 CoreLocation 提供的地理編碼器,Geo 取自 Geography。它負責在「地名」與「座標」之間雙向轉換:
兩個核心方法:
- (void)geocodeAddressString:(NSString *)addressString
completionHandler:(CLGeocodeCompletionHandler)completionHandler;
- (void)reverseGeocodeLocation:(CLLocation *)location
completionHandler:(CLGeocodeCompletionHandler)completionHandler;
無論成功或失敗,操作結束後都會觸發 CLGeocodeCompletionHandler 這個 block,它帶回兩個參數:
CLPlacemark 物件。CLPlacemark 字面意思是「地標」,封裝了完整的地址資訊,常用屬性包括:
@property (nonatomic, readonly) CLLocation *location; // 地理座標 @property (nonatomic, readonly) CLRegion *region; // 行政區域 @property (nonatomic, readonly) NSDictionary *addressDictionary; // 完整地址字典 @property (nonatomic, readonly) NSString *name; // 地址名稱 @property (nonatomic, readonly) NSString *locality; // 城市 @property (nonatomic, readonly) NSString *country; // 國家 @property (nonatomic, readonly) NSString *postalCode; // 郵遞區號
#import <CoreLocation/CoreLocation.h>
@interface YYViewController ()
@property (nonatomic, strong) CLGeocoder *geocoder;
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;
@end
@implementation YYViewController
// 懶載入:用到時才建立
- (CLGeocoder *)geocoder
{
if (_geocoder == nil) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
- (IBAction)geocode
{
NSString *address = self.addressField.text;
if (address.length == 0) return;
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
// 有錯誤或查無結果,都視為找不到
if (error || placemarks.count == 0) {
self.detailAddressLabel.text = @"你輸入的地址沒找到,可能在月球上";
return;
}
// 逐一檢視所有候選地標
for (CLPlacemark *pm in placemarks) {
NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@",
pm.name, pm.locality, pm.country, pm.postalCode);
}
// 取第一筆顯示
CLPlacemark *first = [placemarks firstObject];
self.detailAddressLabel.text = first.name;
CLLocationDegrees latitude = first.location.coordinate.latitude;
CLLocationDegrees longitude = first.location.coordinate.longitude;
self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f", latitude];
self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f", longitude];
}];
}
@end
- (IBAction)reverseGeocode
{
NSString *longitudeText = self.longitudeField.text;
NSString *latitudeText = self.latitudeField.text;
if (longitudeText.length == 0 || latitudeText.length == 0) return;
CLLocationDegrees latitude = [latitudeText doubleValue];
CLLocationDegrees longitude = [longitudeText doubleValue];
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error || placemarks.count == 0) {
self.reverseDetailAddressLabel.text = @"你輸入的地址沒找到,可能在月球上";
return;
}
CLPlacemark *first = [placemarks firstObject];
self.reverseDetailAddressLabel.text = first.name;
// 把系統解析後的座標回填到輸入框
self.latitudeField.text = [NSString stringWithFormat:@"%.2f", first.location.coordinate.latitude];
self.longitudeField.text = [NSString stringWithFormat:@"%.2f", first.location.coordinate.longitude];
}];
}
// 點擊空白處收起鍵盤
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}