标签:
地理编码,顾名思义是应用于定位功能,下面简单介绍下CLLocation中的地理编码:
界面:
代码:
1 #import "HMViewController.h" 2 #import <CoreLocation/CoreLocation.h> 3 4 @interface HMViewController () 5 @property (nonatomic, strong) CLGeocoder *geocoder; 6 7 #pragma mark - 地理编码 8 - (IBAction)geocode; 9 @property (weak, nonatomic) IBOutlet UITextField *addressField; 10 @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel; 11 @property (weak, nonatomic) IBOutlet UILabel *latitudeLabel; 12 @property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel; 13 14 #pragma mark - 反地理编码 15 - (IBAction)reverseGeocode; 16 @property (weak, nonatomic) IBOutlet UITextField *longtitudeField; 17 @property (weak, nonatomic) IBOutlet UITextField *latitudeField; 18 @property (weak, nonatomic) IBOutlet UILabel *reverseDetailAddressLabel; 19 @end 20 21 @implementation HMViewController 22 23 - (CLGeocoder *)geocoder 24 { 25 if (!_geocoder) { 26 self.geocoder = [[CLGeocoder alloc] init]; 27 } 28 return _geocoder; 29 } 30 31 - (void)viewDidLoad 32 { 33 [super viewDidLoad]; 34 35 } 36 37 /** 38 * 地理编码:地名 -> 经纬度 39 */ 40 - (void)geocode 41 { 42 // 1.获得输入的地址 43 NSString *address = self.addressField.text; 44 if (address.length == 0) return; 45 46 // 2.开始编码 47 [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { 48 if (error || placemarks.count == 0) { 49 self.detailAddressLabel.text = @"你输入的地址找不到,可能在火星上"; 50 } else { // 编码成功(找到了具体的位置信息) 51 // 输出查询到的所有地标信息 52 for (CLPlacemark *placemark in placemarks) { 53 NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@", placemark.name, placemark.locality, placemark.country, placemark.postalCode); 54 } 55 56 // 显示最前面的地标信息 57 CLPlacemark *firstPlacemark = [placemarks firstObject]; 58 self.detailAddressLabel.text = firstPlacemark.name; 59 60 CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude; 61 CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude; 62 self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f", latitude]; 63 self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f", longitude]; 64 } 65 }]; 66 } 67 68 /** 69 * 反地理编码:经纬度 -> 地名 70 */ 71 - (void)reverseGeocode 72 { 73 NSString *longtitudeText = self.longtitudeField.text; 74 NSString *latitudeText = self.latitudeField.text; 75 if (longtitudeText.length == 0 || latitudeText.length == 0) return; 76 77 CLLocationDegrees latitude = [latitudeText doubleValue]; 78 CLLocationDegrees longtitude = [longtitudeText doubleValue]; 79 80 // 开始反向编码 81 CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longtitude]; 82 [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { 83 if (error || placemarks.count == 0) { 84 self.reverseDetailAddressLabel.text = @"你输入的经纬度找不到,可能在火星上"; 85 } else { // 编码成功(找到了具体的位置信息) 86 // 输出查询到的所有地标信息 87 for (CLPlacemark *placemark in placemarks) { 88 NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@", placemark.name, placemark.locality, placemark.country, placemark.postalCode); 89 } 90 91 // 显示最前面的地标信息 92 CLPlacemark *firstPlacemark = [placemarks firstObject]; 93 self.reverseDetailAddressLabel.text = firstPlacemark.name; 94 95 CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude; 96 CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude; 97 self.latitudeField.text = [NSString stringWithFormat:@"%.2f", latitude]; 98 self.longtitudeField.text = [NSString stringWithFormat:@"%.2f", longitude]; 99 } 100 }]; 101 } 102 103 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 104 { 105 [self.view endEditing:YES]; 106 } 107 108 @end
标签:
原文地址:http://www.cnblogs.com/zwois/p/4637996.html