#import
- (void)viewDidLoad {
[super viewDidLoad];
_tv.backgroundColor = [UIColor yellowColor];
self.geocoder = [[CLGeocoder alloc] init];
}
- (IBAction)btn1:(UIButton *)sender {
// __block ViewController *weakSelf = self;
NSString *addr = self.tf1.text;
if (addr != nil && addr.length > 0) {
[self.geocoder geocodeAddressString:self.tf1.text completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0) {
CLPlacemark *place = placemarks[0];
CLLocation *location = place.location;
self.tv.text = [NSString stringWithFormat:@"%@经度为:%g,纬度为:%g",addr,location.coordinate.longitude,location.coordinate.latitude];
// CLLocationCoordinate2D coor = location.coordinate;
// [self.tv setText:@"纬度:%@\n经度:%@",coor.location,];
}else
{
[[[UIAlertView alloc] initWithTitle:@"提示框" message:@"地址无法解析" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil] show];
}
}];
}}
- (IBAction)btn2:(UIButton *)sender {
NSString *longitudeStr = self.tf2.text;
NSString *latitudeStr = self.tf3.text;
if (longitudeStr != nil && longitudeStr.length > 0 && latitudeStr != nil && latitudeStr.length > 0) {
CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitudeStr floatValue] longitude:[longitudeStr floatValue]];
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0) {
CLPlacemark *placemark = placemarks[0];
NSArray *addrArray = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
NSMutableString *addr = [[NSMutableString alloc] init];
for (int i = 0; i < addrArray.count; i++) {
[addr appendString:addrArray[i]];
}
self.tv.text = [NSString stringWithFormat:@"经度:%g,纬度:%g的地址为:%@",location.coordinate.longitude,location.coordinate.latitude,addr];
}
}];
}
}
@end