您的位置:首页 > 移动开发 > IOS开发

地址转经纬度(IOS自带方法)

2015-12-24 11:48 393 查看
地址转经纬度(IOS自带方法)

1、导入头文件

#import <CoreLocation/CoreLocation.h>

2、定义全局变量

//定义CLGeocoder反向地理编码

CLGeocoder *_geocoder;

//传给接口的经纬度

NSString *_latitude;//纬度

NSString *_longitude;//经度

3、在viewDidLoad中初始化_geocoder,并调用地址转经纬度方法

- (void)viewDidLoad {

[super viewDidLoad];

_geocoder=[[CLGeocoder alloc]init];

[self getCoordinateByAddress:@"上海市徐汇区漕溪北路41号"];

}

4、地址转经纬度方法(_longitude,_latitude就是地址解析后的经纬度)

#pragma mark 根据地名确定地理坐标

-(void)getCoordinateByAddress:(NSString *)address{

//地理编码

[_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {

//取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址

CLPlacemark *placemark=[placemarks firstObject];

CLLocation *location=placemark.location;//位置

CLRegion *region=placemark.region;//区域

NSDictionary *addressDic= placemark.addressDictionary;//详细地址信息字典,包含以下部分信息

// NSString *name=placemark.name;//地名

// NSString *thoroughfare=placemark.thoroughfare;//街道

// NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等

// NSString *locality=placemark.locality; // 城市

// NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑

// NSString *administrativeArea=placemark.administrativeArea; // 州

// NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息

// NSString *postalCode=placemark.postalCode; //邮编

// NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码

// NSString *country=placemark.country; //国家

// NSString *inlandWater=placemark.inlandWater; //水源、湖泊

// NSString *ocean=placemark.ocean; // 海洋

// NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标

NSLog(@"位置:%@,区域:%@,详细信息:%@",location,region,addressDic);

CLLocationDegrees latitude=location.coordinate.latitude;

CLLocationDegrees longitude=location.coordinate.longitude;

NSLog(@"纬度-->%lf,经度-->%lf",latitude,longitude);

//传给接口的纬度和经度

_latitude=[NSString stringWithFormat:@"%lf",latitude];

_longitude=[NSString stringWithFormat:@"%lf",longitude];

}];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: