//git
// ViewController.mide
// 地理編碼以及反地理編碼編碼
//spa
// Created by DC017 on 15/12/23..net
// Copyright © 2015年 DC017. All rights reserved.3d
//代理
//搜索的全部結果都是在中國境內,由於蘋果在中國的地圖商務代理是高德地圖code
#import "ViewController.h"orm
#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;
}
@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