地理编码 与 反地理编码让你便于理解的效果图如下,这些控件相信大家都会敲这里就不多说了,主要看两种编码的代码

#pragma mark——地理编码 控件——
buttonCoding = [UIButton buttonWithType:UIButtonTypeCustom];
buttonCoding.frame = CGRectMake(20, 50, 120, 30);
buttonCoding.backgroundColor = [UIColor redColor];
[buttonCoding setTitle:@"地理编码" forState: UIControlStateNormal];
[buttonCoding addTarget:self action:@selector(geocodeAddress) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonCoding];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 100, 80, 30)];
label.text = @"地址";
[self.view addSubview:label];
textCoding = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
textCoding.keyboardType = UIKeyboardTypeDefault;
textCoding.delegate = self;
textCoding.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:textCoding];
#pragma mark——地理编码 方法——
- (void)geocodeAddress {
NSString *addName = textCoding.text;
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
[geoCoder geocodeAddressString:addName completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"您输入的地址不存在,请重新输入");
}else{
for (CLPlacemark *placeMarkXX in placemarks) {
NSLog(@"%@ %@ 经度是:%f 纬度是:%f",placeMarkXX.country,placeMarkXX.name,placeMarkXX.location.coordinate.longitude,placeMarkXX.location.coordinate.latitude);
}
}
}];
}
#pragma mark——反地理编码 控件——
againstButtonCoding = [UIButton buttonWithType:UIButtonTypeCustom];
againstButtonCoding.frame = CGRectMake(20, 200, 120, 30);
againstButtonCoding.backgroundColor = [UIColor redColor];
[againstButtonCoding setTitle:@"反地理编码" forState: UIControlStateNormal];
[againstButtonCoding addTarget:self action:@selector(reverseGeocode) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:againstButtonCoding];
UILabel *reverse000 = [[UILabel alloc]initWithFrame:CGRectMake(20, 250, 60, 30)];
reverse000.text = @"经度";
[self.view addSubview:reverse000];
reversetextCoding00 = [[UITextField alloc]initWithFrame:CGRectMake(90, 250, 100, 30)];
reversetextCoding00.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:reversetextCoding00];
UILabel *reverse001 = [[UILabel alloc]initWithFrame:CGRectMake(200, 250, 60, 30)];
reverse001.text = @"纬度";
[self.view addSubview:reverse001];
reversetextCoding11 = [[UITextField alloc]initWithFrame:CGRectMake(270, 250, 100, 30)];
reversetextCoding11.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:reversetextCoding11];
#pragma mark——反地理编码方法——
- (void)reverseGeocode {
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
CLLocationDegrees latitude = [reversetextCoding11.text doubleValue];
CLLocationDegrees longitude = [reversetextCoding00.text doubleValue];
CLLocation *cllocationxx = [[CLLocation alloc]initWithLatitude: latitude longitude:longitude];
[geoCoder reverseGeocodeLocation:cllocationxx completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"经度:0°~180°,纬度0°~90°,请重新输入");
}else{
for (CLPlacemark *reveser in placemarks) {
NSLog(@"%@ %@ %@",reveser.country,reveser.locality,reveser.name);
}
}
}];
}
完了之后在模拟器上随便输入一个地址 或者 经纬度试试