#import "ViewController.h"
#import "MBProgressHUD+MJ.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
@property (nonatomic, strong) CLGeocoder *geocoder; // 编码对象
@property (weak, nonatomic) IBOutlet UILabel *detailLabel; // 详细地址
@property (weak, nonatomic) IBOutlet UITextField *latitudeField; // 纬度
@property (weak, nonatomic) IBOutlet UITextField *longitudeField; // 经度
@end
@implementation ViewController
/**
* 反地理编码
*
*/
- (IBAction)encoderBtn:(id)sender
{
// 1. 获取用户输入的经纬度
NSString *longitude = self.longitudeField.text;
NSString *latitude = self.latitudeField.text;
if (longitude.length == 0 || longitude == nil || latitude.length == 0 || latitude == nil) {
[MBProgressHUD showError:@"请输入经纬度" toView:self.view];
return;
}
// 2. 创建CLLocation对象
CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];
// 3. 反编码 - 输出地标信息
/**
* 反编码
*
* @param CLLocation - 有经度纬度等信息
*/
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *placemark in placemarks) {
LogRed(@"%@ -- %@ --- %f --- %f",placemark.name, placemark.addressDictionary, placemark.location.coordinate.longitude, placemark.location.coordinate.latitude);
NSMutableString *mutstr = [NSMutableString string];
for (NSString *subStr in placemark.addressDictionary[@"FormattedAddressLines"]) {
[mutstr appendString:subStr];
}
LogGreen(@"%@",mutstr);
}
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
//
}
/**
* 懒加载
*/
- (CLGeocoder *)geocoder{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}