》CLGeocoder对象
》三种编码方案
》CLPlacemark讲解(locality:城市名称 thoroughfare:街道名称 name:全称 CLLocation *location)
》反地理编码
》三种编码方案
》CLPlacemark讲解(locality:城市名称 thoroughfare:街道名称 name:全称 CLLocation *location)
》反地理编码
//
// ViewController.m
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *addressTV;
@property (weak, nonatomic) IBOutlet UITextField *laTF;
@property (weak, nonatomic) IBOutlet UITextField *longTF;
/** 地理编码 */
@property (nonatomic, strong) CLGeocoder *geoC;
@end
@implementation ViewController
- (CLGeocoder *)geoC
{
if (!_geoC) {
_geoC = [[CLGeocoder alloc] init];
}
return _geoC;
}
/**
* 地理编码
*/
- (IBAction)geoCoder {
NSString *addr = self.addressTV.text;
if ([addr length] == 0) {
return;
}
[self.geoC geocodeAddressString:@"小码哥" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
/**
* CLPlacemark
location : 位置对象
addressDictionary : 地址字典
name : 地址全称
*/
if(error == nil)
{
NSLog(@"%@", placemarks);
[placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@", obj.name);
self.addressTV.text = obj.name;
self.laTF.text = @(obj.location.coordinate.latitude).stringValue;
self.longTF.text = @(obj.location.coordinate.longitude).stringValue;
}];
}else
{
NSLog(@"cuowu--%@", error.localizedDescription);
}
}];
}
/**
* 反地理编码
*/
- (IBAction)reverseGeoCoder {
double lati = [self.laTF.text doubleValue];
double longi = [self.longTF.text doubleValue];
// TODO: 容错
CLLocation *loc = [[CLLocation alloc] initWithLatitude:lati longitude:longi];
[self.geoC reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if(error == nil)
{
NSLog(@"%@", placemarks);
[placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@", obj.name);
self.addressTV.text = obj.name;
self.laTF.text = @(obj.location.coordinate.latitude).stringValue;
self.longTF.text = @(obj.location.coordinate.longitude).stringValue;
}];
}else
{
NSLog(@"cuowu");
}
}];
}
@end
本文介绍如何使用iOS中的CLGeocoder进行地理编码和反地理编码操作。通过实例展示了如何将地址转换为经纬度坐标,并从经纬度坐标获取详细地址信息。
5663

被折叠的 条评论
为什么被折叠?



