// 地理编码以及反地理编码12-23
//
// Created by dc004 on 15/12/23.
// Copyright © 2015年 gang. All rights reserved.
//
//搜索的所有结果都是在中国境内的,因为苹果在中国的地理服务商是高德地图
#import "ViewController.h"
//引入头文件
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
{
CLGeocoder *_geoCoder;
UILabel *label;
UILabel *label1;
UILabel *label2;
UILabel *label3;
UILabel *label4;
UILabel *label5;
UILabel *label6;
UILabel *label7;
UILabel *label8;
UILabel *label9;
UITextField *text1;
UITextField *text2;
UIButton *button;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_geoCoder = [[CLGeocoder alloc]init];
button = [[UIButton alloc]initWithFrame:CGRectMake(185, 500, 80, 40)];
[button setTitle:@"查询" forState:UIControlStateNormal];
[button setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(consult) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
label = [[UILabel alloc]initWithFrame:CGRectMake(75, 100, 300, 50)];
label.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:0.8];
label.numberOfLines = 3;
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
label3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 75, 50)];
label3.text = @"地址:";
label3.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label3];
label4 = [[UILabel alloc]initWithFrame:CGRectMake(0, 160, 75, 50)];
label4.text = @"经度:";
label4.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label4];
label5 = [[UILabel alloc]initWithFrame:CGRectMake(0, 220, 75, 50)];
label5.text = @"纬度:";
label5.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label5];
label1 = [[UILabel alloc]initWithFrame:CGRectMake(75, 160, 300, 50)];
label1.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:0.8];
label1.numberOfLines = 3;
label1.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label1];
label2 = [[UILabel alloc]initWithFrame:CGRectMake(75, 220, 300, 50)];
label2.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:0.8];
label2.numberOfLines = 3;
label2.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label2];
label6 = [[UILabel alloc]initWithFrame:CGRectMake(75, 280, 300, 50)];
label6.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:0.8];
label6.numberOfLines = 3;
label6.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label6];
label7 = [[UILabel alloc]initWithFrame:CGRectMake(0, 280, 75, 50)];
label7.text = @"海拔:";
label7.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label7];
label8 = [[UILabel alloc]initWithFrame:CGRectMake(0, 380, 75, 50)];
label8.text = @"经度:";
label8.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label8];
label9 = [[UILabel alloc]initWithFrame:CGRectMake(0, 440, 75, 50)];
label9.text = @"纬度:";
label9.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label9];
text1 = [[UITextField alloc]initWithFrame:CGRectMake(75, 380, 300, 50)];
text1.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:0.8];
text1.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:text1];
text2 = [[UITextField alloc]initWithFrame:CGRectMake(75, 440, 300, 50)];
text2.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:0.8];
text2.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:text2];
//地理编码。。。传入的信息是地名
// [self getCoordinateByAddress:@"海口市秀英区"];
//反地理编码。。。传入的信息是坐标(经纬度)
[self getAddressByLatitude:[text1.text floatValue] longitude:[text2.text floatValue]];
}
//通过地名
#pragma mark 根据地名确定地理坐标
-(void)getCoordinateByAddress : (NSString *)address{
[_geoCoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//取得地标,地标中储存了详细的地址信息。注意:一个地名可以搜索出多个地址
CLPlacemark *placemark = [placemarks firstObject];
NSLog(@"%@",placemark);
//剖析地标
//位置
CLLocation *location = placemark.location;
NSLog(@"位置%@",location);
CLLocationCoordinate2D coordinate = location.coordinate;
label1.text =[NSString stringWithFormat:@"%.2f",coordinate.latitude];
label2.text =[NSString stringWithFormat:@"%.2f",coordinate.longitude];
label6.text = [NSString stringWithFormat:@"%.2f",location.altitude];
//区域
CLRegion *region = placemark.region;
NSLog(@"区域%@",region);
//详细地址
NSDictionary *addressDict = placemark.addressDictionary;
NSLog(@"%@",addressDict);
label.text = addressDict[@"Name"];
}];
}
//通过坐标
#pragma mark 根据坐标确定地名
-(void)getAddressByLatitude: (CLLocationDegrees)latitude longitude : (CLLocationDegrees)longitude{
//通过经纬度创建位置信息
CLLocation *location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
[_geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//获取地标
CLPlacemark *placemark = [placemarks firstObject];
//打印地标中的地理信息
NSLog(@"%@",placemark.addressDictionary);
label.text =placemark.addressDictionary[@"FormattedAddressLines"][0];
}];
}
-(void)consult{
[self getAddressByLatitude:[text1.text floatValue]longitude:[text2.text floatValue]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
转载于:https://my.oschina.net/u/2499339/blog/608062