- 使用CLGeocoder可以完成“地理編碼”和“反地理編碼”
- 地理編碼:根據(jù)給定的地名阴孟,獲得具體的位置信息(比如經(jīng)度和緯度,以及地址的全稱)
- 反地理編碼:根據(jù)給定的經(jīng)度和緯度稽坤,獲取具體的位置信息
(一)地理編碼
-
具體的做法:(就兩步)
1.創(chuàng)建地理編碼對(duì)象:導(dǎo)入框架#import <CoreLocation/CoreLocation.h> CLGeocoder *geocoder = [[CLGeocoder alloc]init]; 2.利用地理編碼對(duì)象編碼:聲明一個(gè)@property(nonatomic,strong) CLGeocoder *geocoder;屬性
調(diào)用一個(gè)地理編碼方法:(很重要)
[self.geocoder geocodeAddressString:self.addressFiled.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error)
地理編碼方法:
下面是具體的代碼:(在storyBoard里面畫的剩拢,1個(gè)button,1個(gè)UITextFiled,3個(gè)UILabel)
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
/*
需要編碼的地址容器
*/
@property (weak, nonatomic) IBOutlet UITextField *addressFiled;
/*
經(jīng)度容器
*/
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
/*
緯度容器
*/
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
/*
詳情容器
*/
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;
/*
監(jiān)聽(tīng)地理編碼點(diǎn)擊事件
*/
- (IBAction)geocoderBtnClick;
@property(nonatomic,strong) CLGeocoder *geocoder;
@end
@implementation ViewController
//1.創(chuàng)建地理編碼對(duì)象
-(CLGeocoder *)geocoder
{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc]init];
}
return _geocoder;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.addressFiled.clearButtonMode = UITextFieldViewModeAlways;
self.addressFiled.placeholder = @" 請(qǐng)輸入地理位置";
}
- (IBAction)geocoderBtnClick {
//獲取用戶輸入的位置
NSString *addressString = self.addressFiled.text;
NSLog(@"%@",addressString);
if (addressString == nil || addressString.length == 0) {
NSLog(@"請(qǐng)輸入地址");
return;
}
//2.利用地理編碼對(duì)象編碼
//根據(jù)傳入的地址獲取該地址的對(duì)應(yīng)的經(jīng)緯度信息
[self.geocoder geocodeAddressString:self.addressFiled.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//placemarks 地標(biāo) 地標(biāo)數(shù)組里面存放著地標(biāo),每一個(gè)地標(biāo)包含了該位置的經(jīng)緯度够话,以及城市/區(qū)域/國(guó)家代碼/郵編等等....
if (placemarks.count == 0 || error != nil) {
return;
}
//獲取數(shù)組里面第一個(gè)信息
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.創(chuàng)建地理編碼對(duì)象:導(dǎo)入框架#import <CoreLocation/CoreLocation.h>
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
2.利用地理編碼對(duì)象編碼:聲明一個(gè)@property(nonatomic,strong) CLGeocoder *geocoder;屬性
調(diào)用一個(gè)反地理編碼方法:(很重要)
[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.獲取用戶輸入的經(jīng)緯度 NSString *latitudeString = self.latitudeValue.text; NSString *longitudeValueString = self.longitudeValue.text; if (latitudeString == nil || latitudeString.length == 0 || longitudeValueString == nil || longitudeValueString.length == 0) { NSLog(@"請(qǐng)輸入經(jīng)緯度"); return; } //2.根據(jù)用戶輸入的經(jīng)緯度創(chuàng)建CLLocation對(duì)象 CLLocation *location = [[CLLocation alloc]initWithLatitude:[latitudeString doubleValue] longitude:[longitudeValueString doubleValue]]; //3.根據(jù)CLLocation對(duì)象獲取相應(yīng)的地表信息 [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { for (CLPlacemark *placemark in placemarks) { self.labelText.text = placemark.locality; NSLog(@"哈哈"); } }]; } @end