iOS -CoreLocation框架(反)地理编码
#import "ViewController.h"#import@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;
}
#pragma mark -地理编码
- (IBAction)geoCoder {
NSString *addr = self.addressTV.text;
if ([addr length] == 0) {
return;
} [self.geoC geocodeAddressString:addr completionHandler:^(NSArray* _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);
}
}];
}
#pragma mark- 反地理编码
- (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* _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");
}
}];
}