您的位置:首页 > 其它

地理编码和反地理编码

2015-11-08 14:59 351 查看


//
//  ViewController.m

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()
@property(nonatomic,strong)CLGeocoder *geocoder;

#pragma mark - 地理编码
- (IBAction)geocode:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;

#pragma mark - 反地理编码
- (IBAction)reverseGeocode:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UITextField *longitudeField;
@property (weak, nonatomic) IBOutlet UITextField *latitudeField;
@property (weak, nonatomic) IBOutlet UILabel *reverseDatailAddressLabel;

@end

@implementation ViewController

/**
*  延迟创建CLGeocoder对象
*/
- (CLGeocoder *)geocoder
{
if (!_geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}

- (void)viewDidLoad {
[super viewDidLoad];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//退出键盘
[self.view endEditing:YES];
}

/**
*  地理编码
*/
- (IBAction)geocode:(UIButton *)sender {
NSString *address = self.addressField.text;
if(address.length == 0) return;

[self.geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error) { //有错误(地址乱输入)
self.detailAddressLabel.text = @"你找的地址可能只在火星有!!!";
}else{ //编码成功

//取出最前面的地址
CLPlacemark *pm = [placemarks firstObject];
// 设置经纬度
self.latitudeLabel.text = [NSString stringWithFormat:@"%.1f",pm.location.coordinate.latitude];
self.latitudeLabel.text = [NSString stringWithFormat:@"%.1f",pm.location.coordinate.longitude];
//设置具体的位置
self.detailAddressLabel.text = pm.name;

//            NSLog(@"总共找到%lu个地址",placemarks.count);
//
//            for (CLPlacemark *pm in placemarks) {
//                NSLog(@"----地址开始----");
//
//                NSLog(@"%f %f %@",pm.location.coordinate.latitude,pm.location.coordinate.longitude,pm.name);
//
//                //遍历字典
//                [pm.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
//                    NSLog(@"%@ %@",key,obj);
//                }];
//
//                  NSLog(@"----地址结束----");
//            }
}
}];

}

/**
*  反地理编码
*/
- (IBAction)reverseGeocode:(UIButton *)sender {

//1.包装位置
CLLocationDegrees latitude = [self.latitudeLabel.text doubleValue];
CLLocationDegrees longitude = [self.longitudeField.text doubleValue];

CLLocation *loc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

//2.反地理编码
[self.geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error) { //有错误(地址乱输入)
self.reverseDatailAddressLabel.text = @"你找的地址可能只在火星有!!!";
}else{ //编码成功

NSLog(@"总共找到%lu个地址",placemarks.count);
for (CLPlacemark *pm in placemarks) {
NSLog(@"----地址开始----");

NSLog(@"%f %f %@",pm.location.coordinate.latitude,pm.location.coordinate.longitude,pm.name);

//遍历字典
[pm.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"%@ %@",key,obj);
}];

NSLog(@"----地址结束----");
}
}

}];
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: