您的位置:首页 > 移动开发 > Objective-C

OBjective-c 地理编码以及反地理编码

2015-12-23 00:00 417 查看
//
// ViewController.m
// 地理编码以及反地理编码
//
// Created by DC017 on 15/12/23.
// Copyright © 2015年 DC017. All rights reserved.
//
//搜索的所有结果都是在中国境内,因为苹果在中国的地图商务代理是高德地图
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#define CHI_CHUN(x,y,w,h) CGRectMake(x, y, w, h)
#define YAN_SE(r,g,b,a) [UIColor colorWithRed:r/225.0 green:g/225.0 blue:b/225.0 alpha:225.0]
@interface ViewController (){
UITextField * jingDuShuRu;
UITextField * weiDuShuRu;
UITextField * weiZhiShuRu;
CLGeocoder * geoCoder;//创建地理编码对象
UILabel * xianShiI;
UILabel * xianshiII;
UILabel * jingDuXianShi;
UILabel * weiDuXianShi;
}
@end
@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self layout];
//地理编码
geoCoder=[CLGeocoder new];
//地理编码。。。。。传入的信息是地名
//[self getDiMing:@"山阳"];
//反地理编码。。。。。传入的信息是坐标
//[self getZuoBiaoJingDu:20 ZuobiaoWeiDu:120];
}
//地理编码。。。。。传入的信息是地名
-(void)getDiMing:(NSString*)diming{

[geoCoder geocodeAddressString:diming completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//取得地标,地标中储存了详细的地址信息,
CLPlacemark *placemark=[placemarks firstObject ];
NSLog(@"%@",placemark);
//剖析地标
//位置
CLLocation * location=placemark.location;
CLLocationCoordinate2D zuobiao=location.coordinate;
NSLog(@"%@",location);
//区域
CLRegion *region=placemark.region;
NSLog(@"%@",region);
//详细地址
NSDictionary * dic=placemark.addressDictionary;
NSLog(@"%@",dic);
NSLog(@"%@",dic[@"Country"]);//国家
NSLog(@"%@",dic[@"Name"]);//省
NSLog(@"%@",dic[@"State"]);

NSLog(@"%@",dic[@"FormattedAddressLines"][0]);//详细地址

xianShiI.text=dic[@"FormattedAddressLines"][0];
jingDuXianShi.text=[NSString stringWithFormat:@"%.2f",zuobiao.latitude];
weiDuXianShi.text=[NSString stringWithFormat:@"%.2f",zuobiao.longitude];

}];

}
//反地理编码。。。。。传入的信息是坐标
-(void)getZuoBiaoJingDu:(CLLocationDegrees)jingDu ZuobiaoWeiDu:(CLLocationDegrees)weiDu{
CLLocation * location=[[CLLocation alloc]initWithLatitude:jingDu longitude:weiDu];
[geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark * placemark=[placemarks firstObject];
NSLog(@"%@",placemark);
NSDictionary * dicII=placemark.addressDictionary;
xianshiII.text=dicII[@"FormattedAddressLines"][0];
}];

}
-(void)layout{
UILabel * dilibianma=[[UILabel alloc]initWithFrame:CHI_CHUN(20, 40, 100, 20)];
UILabel * fandilibianma=[[UILabel alloc]initWithFrame:CHI_CHUN(20, 300, 100, 20)];
UILabel * xiangQing=[[UILabel alloc]initWithFrame:CHI_CHUN(20, 120,100, 20)];
xianShiI=[[UILabel alloc]initWithFrame:CHI_CHUN(20, 160, 335, 30)];
xianShiI.backgroundColor=[UIColor redColor];
xianShiI.textColor=[UIColor blackColor];

UILabel * jingDu1=[[UILabel alloc]initWithFrame:CHI_CHUN(20, 220, 60, 20)];
UILabel * weiDu2=[[UILabel alloc]initWithFrame:CHI_CHUN(200, 220, 60, 20)];
jingDuXianShi=[[UILabel alloc]initWithFrame:CHI_CHUN(60, 220, 80, 20)];
weiDuXianShi=[[UILabel alloc]initWithFrame:CHI_CHUN(240, 220, 80, 20)];

UILabel * labelWeiZhi=[[UILabel alloc]initWithFrame:CHI_CHUN(20, 80, 60, 20)];
UILabel * labelJingDu=[[UILabel alloc]initWithFrame:CHI_CHUN(20, 340, 60, 20)];
UILabel * labelWeiDu=[[UILabel alloc]initWithFrame:CHI_CHUN(20, 380,60, 20)];

dilibianma.text=@"地理编码";
dilibianma.textColor=YAN_SE(142, 601, 60, 1);

fandilibianma.textColor=YAN_SE(600, 80, 660, 1);
fandilibianma.text=@"反地理编码";

xiangQing.text=@"详细地址:";
xiangQing.textColor=[UIColor blackColor];

jingDu1.text=@"经度:";
jingDu1.textColor=[UIColor orangeColor];
weiDu2.text=@"纬度:";
weiDu2.textColor=[UIColor orangeColor];
jingDuXianShi.textColor=[UIColor blackColor];
jingDuXianShi.backgroundColor=[UIColor redColor];
weiDuXianShi.textColor=[UIColor blackColor];
weiDuXianShi.backgroundColor=[UIColor redColor];
weiZhiShuRu=[[UITextField alloc]initWithFrame:CHI_CHUN(65, 80, 200, 20)];
jingDuShuRu=[[UITextField alloc]initWithFrame:CHI_CHUN(65, 340, 200, 20)];
weiDuShuRu=[[UITextField alloc]initWithFrame:CHI_CHUN(65, 380, 200, 20)];
labelWeiZhi.textColor=[UIColor orangeColor];
labelWeiZhi.text=@"位置:";
weiZhiShuRu.layer.borderWidth=1;
weiZhiShuRu.layer.borderColor=YAN_SE(100, 200, 60, 1).CGColor;
weiZhiShuRu.placeholder=@"请输入地理位置";
labelJingDu.textColor=[UIColor orangeColor];
labelJingDu.text=@"经度:";
jingDuShuRu.layer.borderWidth=1;
jingDuShuRu.layer.borderColor=YAN_SE(100, 200, 60, 1).CGColor;
jingDuShuRu.placeholder=@"请输入经度";

labelWeiDu.textColor=[UIColor orangeColor];
labelWeiDu.text=@"纬度:";

weiDuShuRu.layer.borderColor=YAN_SE(100, 200, 60, 1).CGColor;
weiDuShuRu.layer.borderWidth=1;
weiDuShuRu.placeholder=@"请输入纬度";
UILabel * xiangQingII=[[UILabel alloc]initWithFrame:CHI_CHUN(20, 420,100, 20)];
xiangQingII.text=@"详细地址:";
xiangQingII.textColor=[UIColor blackColor];
xianshiII=[[UILabel alloc]initWithFrame:CHI_CHUN(20, 460, 335, 30)];
xianshiII.backgroundColor=[UIColor redColor];
xianshiII.textColor=[UIColor blackColor];
UIButton * queDingI=[[UIButton alloc]initWithFrame:CHI_CHUN(300, 280, 60, 20)];
[queDingI setTitle:@"查询" forState:UIControlStateNormal];
[queDingI setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[queDingI addTarget:self action:@selector(qudingI) forControlEvents:UIControlEventTouchUpInside];

UIButton * queDingII=[[UIButton alloc]initWithFrame:CHI_CHUN(300, 570, 60, 20)];
[queDingII setTitle:@"查询" forState:UIControlStateNormal];
[queDingII setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[queDingII addTarget:self action:@selector(dianjiII) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dilibianma];
[self.view addSubview:fandilibianma];
[self.view addSubview:xiangQing];
[self.view addSubview:jingDu1];
[self.view addSubview:weiDu2];
[self.view addSubview:jingDuXianShi];
[self.view addSubview:weiDuXianShi];
[self.view addSubview:xianShiI];
[self.view addSubview:labelWeiZhi];
[self.view addSubview:weiZhiShuRu];
[self.view addSubview:labelJingDu];
[self.view addSubview:jingDuShuRu];
[self.view addSubview:labelWeiDu];
[self.view addSubview:weiDuShuRu];
[self.view addSubview:xiangQingII];
[self.view addSubview:xianshiII];
[self.view addSubview:queDingI];
[self.view addSubview:queDingII];

}
-(void)qudingI{
[self getDiMing:[NSString stringWithFormat:@"%@",weiZhiShuRu.text]];

}
-(void)dianjiII{
[self getZuoBiaoJingDu:[jingDuShuRu.text floatValue] ZuobiaoWeiDu:[weiDuShuRu.text floatValue]];

}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

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