地理编码反地理编码, 根据地名获取经纬度等信息
2016-07-12 17:43
471 查看
CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写。
1.使用CLGeocoder可以完成“地理编码”和“反地理编码”
地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
反地理编码:根据给定的经纬度,获得具体的位置信息
(1)地理编码方法
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
(2)反地理编码方法
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
2.CLGeocodeCompletionHandler
当地理\反地理编码完成时,就会调用CLGeocodeCompletionHandler
这个block传递2个参数
error :当编码出错时(比如编码不出具体的信息)有值
placemarks :里面装着CLPlacemark对象
3.CLPlacemark
说明:CLPlacemark的字面意思是地标,封装详细的地址位置信息
地理位置 @property (nonatomic,strong) CLLocation *location;
区域 @property (nonatomic,strong) CLRegion *region;
详细的地址信息 @property (nonatomic, strong) NSDictionary *addressDictionary;
地址名称 @property (nonatomic, copy) NSString *name;
城市 @property (nonatomic,copy) NSString *locality;
#import
<CoreLocation/CoreLocation.h>
@property(nonatomic,strong)CLGeocoder
* geocoder;
@property(nonatomic,strong)CLLocation
* location1;
#pragma mark
根据地名确定地理坐标
-(void)getCoordinateByAddress:(NSString
*)address{
//地理编码
[_geocodergeocodeAddressString:addresscompletionHandler:^(NSArray
*placemarks,NSError *error) {
//取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址
CLPlacemark *placemark=[placemarksfirstObject];
self.location1=placemark.location;//位置
CLRegion *region=placemark.region;//区域
NSDictionary *addressDic= placemark.addressDictionary;//详细地址信息字典,包含以下部分信息
NSLog(@"%@",self.location1);
}];
}
#pragma mark 根据地理坐标确定地名
-(void)getAddressByCoordinate:(NSString *)latitudeText with:(NSString *)longtitudeText{
CLLocationDegrees latitude=[latitudeTextdoubleValue];
CLLocationDegrees longitude=[longtitudeTextdoubleValue];
CLLocation *location=[[CLLocationalloc]initWithLatitude:latitudelongitude:longitude];
//2.反地理编码
[self.geocoderreverseGeocodeLocation:locationcompletionHandler:^(NSArray
*placemarks,NSError *error) {
if (error||placemarks.count==0) {
self.reverdeDetailAddressLabel.text=@"你输入的地址没找到";
}else//编码成功
{
//显示最前面的地标信息
CLPlacemark *firstPlacemark=[placemarksfirstObject];
self.reverdeDetailAddressLabel.text=firstPlacemark.name;
//经纬度
CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
self.latitudeField.text=[NSStringstringWithFormat:@"%.2f",latitude];
self.longitudeField.text=[NSStringstringWithFormat:@"%.2f",longitude];
}
}];
1.使用CLGeocoder可以完成“地理编码”和“反地理编码”
地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
反地理编码:根据给定的经纬度,获得具体的位置信息
(1)地理编码方法
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
(2)反地理编码方法
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
2.CLGeocodeCompletionHandler
当地理\反地理编码完成时,就会调用CLGeocodeCompletionHandler
这个block传递2个参数
error :当编码出错时(比如编码不出具体的信息)有值
placemarks :里面装着CLPlacemark对象
3.CLPlacemark
说明:CLPlacemark的字面意思是地标,封装详细的地址位置信息
地理位置 @property (nonatomic,strong) CLLocation *location;
区域 @property (nonatomic,strong) CLRegion *region;
详细的地址信息 @property (nonatomic, strong) NSDictionary *addressDictionary;
地址名称 @property (nonatomic, copy) NSString *name;
城市 @property (nonatomic,copy) NSString *locality;
#import
<CoreLocation/CoreLocation.h>
@property(nonatomic,strong)CLGeocoder
* geocoder;
@property(nonatomic,strong)CLLocation
* location1;
#pragma mark
根据地名确定地理坐标
-(void)getCoordinateByAddress:(NSString
*)address{
//地理编码
[_geocodergeocodeAddressString:addresscompletionHandler:^(NSArray
*placemarks,NSError *error) {
//取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址
CLPlacemark *placemark=[placemarksfirstObject];
self.location1=placemark.location;//位置
CLRegion *region=placemark.region;//区域
NSDictionary *addressDic= placemark.addressDictionary;//详细地址信息字典,包含以下部分信息
NSLog(@"%@",self.location1);
}];
}
#pragma mark 根据地理坐标确定地名
-(void)getAddressByCoordinate:(NSString *)latitudeText with:(NSString *)longtitudeText{
CLLocationDegrees latitude=[latitudeTextdoubleValue];
CLLocationDegrees longitude=[longtitudeTextdoubleValue];
CLLocation *location=[[CLLocationalloc]initWithLatitude:latitudelongitude:longitude];
//2.反地理编码
[self.geocoderreverseGeocodeLocation:locationcompletionHandler:^(NSArray
*placemarks,NSError *error) {
if (error||placemarks.count==0) {
self.reverdeDetailAddressLabel.text=@"你输入的地址没找到";
}else//编码成功
{
//显示最前面的地标信息
CLPlacemark *firstPlacemark=[placemarksfirstObject];
self.reverdeDetailAddressLabel.text=firstPlacemark.name;
//经纬度
CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
self.latitudeField.text=[NSStringstringWithFormat:@"%.2f",latitude];
self.longitudeField.text=[NSStringstringWithFormat:@"%.2f",longitude];
}
}];
相关文章推荐
- Impala5.7.1 build in linux environment(Centos6.6)
- 百度 天气预报API
- 使用js/jquery查找iframe中的
- Ubuntu下MongoDB的安装与使用(离线安装方式|非apt-get)
- 第一次使用Android Studio时你应该知道的一切配置
- 调用MYSQL存储过程实例
- Map集合中value()方法与keySet()、entrySet()区别
- 实现买菜功能(未完成代码)
- Mac上安装MySQL
- redis 五种数据类型的使用场景
- HDU 4101 Ali and Baba
- Docker常用命令整理
- 字符串的格式化
- SpringMVC+myBatis+druid 多数据库
- JAVA正则表达式 Pattern和Matcher(转)
- mybatis源码学习: 动态代理的应用(慢慢改)
- 你真的认为iphone只是一部手机么
- PHP_php.ini_说明详解
- c++ **p的意思是什么呢?大家真正了解**p吗
- 数学基础知识之正弦、余弦