思路很简单:
(1)创建CLLocation对象;
(2)获取要检索的信息(地名,经纬度);
(3)调用编码方法;
- 编码
#import <CoreLocation/CoreLocation.h>
@property (nonatomic ,strong) CLGeocoder *geocoder;
#import <MapKit/MapKit.h>//定位使用
<CLLocationManagerDelegate,MKMapViewDelegate>
@property (strong, nonatomic) MKMapView *mapView;
如果是使用CLLocation 位的时候一定要注意在plist文件中进行设置
Privacy - Location When In Use Usage Description YES
运行的结果,
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>//定位使用的
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>//定位使用的
//编码使用的
@property (nonatomic ,strong) CLGeocoder *geocoder;
@property (strong, nonatomic) IBOutlet UITextField *addressLabel;//编码中输入地址的TF这里在写的时候失误了命名为label了。
@property (strong, nonatomic) IBOutlet UILabel *wdLabel;
@property (strong, nonatomic) IBOutlet UILabel *jdLabel;
@property (strong, nonatomic) IBOutlet UILabel *detailLabel;
//反向编码使用的控件
@property (strong, nonatomic) IBOutlet UITextField *wdTF;
@property (strong, nonatomic) IBOutlet UITextField *jdTF;
@property (strong, nonatomic) IBOutlet UITextView *detailTextV;
//定位使用的
@property (strong, nonatomic) IBOutlet UILabel *locationjdwdLabel;
@property (strong, nonatomic) IBOutlet UILabel *detailMeageLabel;
@property (strong, nonatomic) MKMapView *mapView;
@property (strong , nonatomic) CLLocationManager * locationManager;
@end
@implementation ViewController
/*地理编码的Demo,输入城市的名称,获取经纬度的信息
(1) 根据地址获取经纬度
a,用户输入位置位置,得到经纬度和具体的地址
一 :首先获取用户输入的位置。
二 :创建地理编码对象。
三 :利用地理编码对象编码,根据传入的地址获取该地址对应的经纬度信息。
四 :最后将他们分别显示出来就可以了。
在定位的时候一定要注意在plist文件中进行设置
Privacy - Location When In Use Usage Description YES
*/
- (void)viewDidLoad {
[super viewDidLoad];
}
//定位
- (IBAction)locationButtonClick:(UIButton *)sender {
[self startLocation];
}
-(void)startLocation{
self.locationManager=[[CLLocationManager alloc]init];
if (![CLLocationManager locationServicesEnabled]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的位置服务当前不可用,请打开位置服务后重试" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alertView show];
return;
}
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
if([CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse){
[self.locationManager requestWhenInUseAuthorization];
}
}
self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
[self.view addSubview:self.mapView];
self.mapView.alpha = 0.0;
self.mapView.mapType = MKMapTypeStandard;
self.mapView.delegate = self;
self.mapView.userTrackingMode=MKUserTrackingModeFollow;
}
-(void)endLocation{
self.mapView.hidden = YES;
self.mapView.delegate = nil;
[self.mapView removeFromSuperview];
self.mapView = nil;
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocationCoordinate2D coord = [userLocation coordinate];
NSLog(@"经度:%f,纬度:%f",coord.latitude,coord.longitude);
self.locationjdwdLabel.text = [NSString stringWithFormat:@"纬度:%f,经度:%f",coord.latitude,coord.longitude];
[self endLocation];
CLLocation *newLocation = [userLocation location];
//------------------位置反编码---5.0之后使用-----------------
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *placemarks, NSError *error){
for (CLPlacemark *place in placemarks) {
NSString * locat = [NSString stringWithFormat:@" %@",place.locality];
NSString * name = [NSString stringWithFormat:@"%@",place.name];
self.detailMeageLabel.text = name;
}
}];
}
-(void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
NSLog(@"%@",@"定位失败");
}
//正向编码:根据具体的位置获取经纬度
- (IBAction)firstButtonClick:(UIButton *)sender {
[self geocodeBtnClick];
}
//反向编码:更加经纬度获取具体的位置
- (IBAction)secondButtonClick:(UIButton *)sender {
[self reverseGeocode];
}
- (void)geocodeBtnClick
{
// 0.获取用户输入的位置
NSString *addressStr = self.addressLabel.text;
if (addressStr == nil || addressStr.length == 0) {
NSLog(@"请输入地址");
return;
}
// 1.创建地理编码对象
// 2.利用地理编码对象编码
// 根据传入的地址获取该地址对应的经纬度信息
[self.geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks.count == 0 || error != nil) {
return ;
}
// placemarks地标数组, 地标数组中存放着地标, 每一个地标包含了该位置的经纬度以及城市/区域/国家代码/邮编等等...
// 获取数组中的第一个地标
CLPlacemark *placemark = [placemarks firstObject];
self.wdLabel.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude];
self.jdLabel.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];
for (CLPlacemark *place in placemarks) {
NSString * locat = [NSString stringWithFormat:@" %@",place.locality];
NSString * name = [NSString stringWithFormat:@"%@",place.name];
NSLog(@"name---%@---------locat------%@",name,locat);
NSLog(@"placemark--------%@----",placemark.addressDictionary);
}
NSArray *address = placemark.addressDictionary[@"FormattedAddressLines"];
NSMutableString *strM = [NSMutableString string];
for (NSString *str in address) {
[strM appendString:str];
}
self.detailLabel.text = strM;
}];
}
- (void)reverseGeocode
{
// 1.获取用户输入的经纬度
NSString *longtitude = self.jdTF.text;
NSString *latitude = self.wdTF.text;
if (longtitude.length == 0 ||
longtitude == nil ||
latitude.length == 0 ||
latitude == nil) {
NSLog(@"请输入经纬度");
return;
}
// 2.根据用户输入的经纬度创建CLLocation对象
CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longtitude doubleValue]];
// 3.根据CLLocation对象获取对应的地标信息
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *placemark in placemarks) {
NSLog(@"%@ %@ %f %f", placemark.name, placemark.addressDictionary, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
self.detailTextV.text = placemark.locality;
}
}];
}
- (CLGeocoder *)geocoder
{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//隐藏键盘
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
整体的思路很简单,具体可以获取到的信息,打印placemark.addressDictionary
就可以查看返回的信息的key值了。