#import "ViewController.h"
@interface ViewController ()
@property (nonatomic , strong )CLLocationManager * locationManager;
@property (weak , nonatomic ) IBOutlet UILabel *outputLongtude;
@property (weak , nonatomic ) IBOutlet UILabel *outputLatitude;
@property (weak , nonatomic ) IBOutlet UILabel *outputLocation;
@property (weak , nonatomic ) IBOutlet UITextField *inputlongtude;
@property (weak , nonatomic ) IBOutlet UITextField *inputLatitude;
@property (weak , nonatomic ) IBOutlet UILabel *displayLongitude;
@property (weak , nonatomic ) IBOutlet UILabel *displayLaitude;
- (IBAction )locationCovertAction:(UIButton *)sender;
- (IBAction )geocoderConverAction:(id )sender;
@property (weak , nonatomic ) IBOutlet UILabel *displayLocation;
@property (weak , nonatomic ) IBOutlet UITextField *inputLocation;
@end
@implementation ViewController
- (void )viewDidLoad {
[super viewDidLoad];
self .locationManager = [[CLLocationManager alloc]init];
[_locationManager startUpdatingLocation];
[_locationManager requestWhenInUseAuthorization];
self .locationManager .delegate = self ;
}
//实现协议中的方法,位置跟新走这个代理方法
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
// NSLog(@"%@" ,locations)
CLLocation * laoction = locations.firstObject
//打印经纬度
//
// NSLog(@"%f" ,laoction.coordinate .latitude )
// NSLog(@"%f" ,laoction.coordinate .longitude )
//
self.outputLongtude .text = [NSString stringWithFormat:@"%f" ,laoction.coordinate .longitude ]
self.outputLatitude .text = [NSString stringWithFormat:@"%f" ,laoction.coordinate .latitude ]
// self.outputLocation .text = [NSString stringWithFormat:@"%@" ,laoction]
//地理编码 反地理编码
CLGeocoder * geo = [[CLGeocoder alloc]init]
//反地理编码(把经纬度转化为位置信息)
[geo reverseGeocodeLocation:laoction completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark * mark = placemarks.firstObject
self.outputLocation .text = mark.name
// NSLog(@"%@" ,mark.name )
}]
}
-(void )touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self .view endEditing:YES ];
}
- (void )didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction )locationCovertAction:(UIButton *)sender {
CLGeocoder * geo = [[CLGeocoder alloc]init];
__weak ViewController * VC = self ;
[geo geocodeAddressString:self .inputLocation .text completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark * mark = placemarks.firstObject ;
VC.displayLaitude .text = [NSString stringWithFormat:@"%f" ,mark.location .coordinate .latitude ];
VC.displayLongitude .text = [NSString stringWithFormat:@"%f" ,mark.location .coordinate .longitude ];
}];
}
- (IBAction )geocoderConverAction:(id )sender {
CLLocation * location = [[CLLocation alloc]initWithLatitude:[self .inputLatitude .text doubleValue] longitude:[self .inputlongtude .text doubleValue]];
CLGeocoder * geo = [[CLGeocoder alloc]init];
[geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark * mark = placemarks.firstObject ;
self .displayLocation .text = mark.name ;
}];
}
@end