iOS开发之 使用CLGeocoder实现经纬度和地址互查

  1. 添加CoreLocation.framework框架

  2. 导入#import <CoreLocation/CoreLocation.h>

  3. 通过地址查询经纬度方法:

  4.     CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:@"湖北襄阳" completionHandler:^(NSArray* placemarks, NSError* error){
            if (!error) {
                
                for (CLPlacemark* aPlacemark in placemarks)
                {
                    NSLog(@"place--%@", [aPlacemark locality]);
                    NSLog(@"lat--%f--lon--%f",aPlacemark.location.coordinate.latitude,aPlacemark.location.coordinate.longitude);
                }
            }
            else{
                
                NSLog(@"error--%@",[error localizedDescription]);
            }
        }];

5.  通过经纬度查询地址方法:

 CLLocation * newLocation = [[CLLocation alloc]initWithLatitude:30.590865 longitude:104.061792];
    
    NSLog(@"-------根据经纬度反查地理位置--%f--%f",senderCoordinate.latitude,senderCoordinate.longitude);
    
    CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
    [clGeoCoder reverseGeocodeLocation:newLocation completionHandler: ^(NSArray *placemarks,NSError *error) {
        
        NSLog(@"--array--%d---error--%@",(int)placemarks.count,error);
        
        if (placemarks.count > 0) {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            NSString *city = placemark.administrativeArea;
            NSLog(@"位于:%@",city);
            NSLog(@"%@",placemark.addressDictionary[@"Name"]);
        }
    }];


你可能感兴趣的:(ios,CLGeocoder)