地址转经纬度(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];
}];
}
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];
}];
}
相关文章推荐
- iOS 之自动布局
- nagios监控国外主机超时
- IOS调用系统相机后状态栏消失BUG
- iOS中idp证书如何给另一台机子使用
- 再谈iOS 7的左边缘手势右滑动返回功能
- iOS alloc 、retain、release函数
- iOS tableview分割线从边框顶端开始
- ios 判断类型
- IOS 收集崩溃信息 NSException类
- iOS屏幕旋转
- 【iOS地图开发】巧妙打造中英文全球地图
- iOS开发系列--地图与定位
- IOS开发证书和代码签名
- IOS 实现QQ好友分组展开关闭功能
- 151224 iOS 推送通知处理
- iOS Xcode编译Undefined symbols for architecture xxx 错误总结
- iOS开发61- CocoaPods安装和使用
- iostat命令
- iOS完整学习路线图
- iOS开发-如何把广告,图片通过代码加到Screen上