码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-08-29 11:18:28      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

  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实现经纬度和地址互查

标签:

原文地址:http://my.oschina.net/linxiaoxi1993/blog/498919

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!