欢迎转载,转载请注明出处
本文地址:http://blog.csdn.net/zhenggaoxing/article/details/42535163
前言:什么是地理信息编码?
地理信息编码:就是通过联系经度纬度的一种对照表。我们可以通过经度纬度信息查找到相应的地址,也可以把相关的地址转换成相应的经度纬度。下面是我理解的地理信息模型:(余则成-密码本模型)
讲解一下:1 在IOS中涉及到地里信息编码的又两个类: CLPlacemark(翻译:地标)和 CLGeocoder类(翻译:地理信息编码)2 其中CLPlacemark类里面存储着对照信息,相当于一个密码本3 CLGeocoder类里面则相当于试用密码本的人 他可以拿着密码本翻译出需要的信息,也可以把需要的信息加密~
实现地理信息反编码
首先,我们实现搞出来一个密码本(CLPlacemark对象)
<span style="font-size:14px;"> CLPlacemark *placemark=placemarks[0];//placemarks 数组类型,存储ClPlacemark对象</span>
然后,获取确定获取密码本的那些信息
<span style="font-size:14px;"> NSDictionary *addressDictionary=placemark.addressDictionary;//获取了地址字典信息 </span>
密码本可以取到一下这些类型的信息:
addressDictionary:
地址信息字典,NSDictortionary类型包含一些键值(只读)ISOcountryCode:
国家代码,NSString 类型 (只读)country:
国家名称,NSString 类型(只读)postalCode:
邮政编码,NSString 类型(只读)administrativeArea
:通过地标找到的州或者省信息,NSString 类型(只读)subAdministrativeArea:
通过地标找到的州或者省信息的附加信息,NSString 类型(只读)
locality:
地标城市信息,NSString 类型(只读)
subLocality:
地标城市的附加信息,NSString 类型(只读)
thoroughfare:
街道信息,NSString 类型(只读)
subThoroughfare:
街道附加信息,NSString 类型(只读)
余则成(CLGeocoder)来对密码本进行操作
- 首先初始化一个CLGeocoder的对象
<span style="font-weight: normal;">CLGeocoder *geocoder =[[CLGeocoder alloc]init];</span>
- 然后通过位置来获取密码本上的信息
解释一下:函数reverseGeocodeLocaiton completionHander 是CLGeocoder的方法,通过CLLocation对象查询相关信息,下面是函数原型,和对函数的翻译</pre></li></ul><li><pre name="code" class="objc"><span style="font-weight: normal;"> [geocoder reverseGeocodeLocation:(CLLocation *)currentLocation completionHandler:^(NSArray *placemarks, NSError *error){ if([placemarks count]>0) { CLPlacemark *placemark=placemarks[0];//placemarks 数组类型,存储ClPlacemark对象 NSDictionary *addressDictionary=placemark.addressDictionary;//获取了地址字典信息 NSString *address = [addressDictionary objectForKey:(NSString *)kABPersonAddressStreetKey]; address = address == nil ? @"": address; NSString *state = [addressDictionary objectForKey:(NSString *)kABPersonAddressStateKey]; state = state == nil ? @"": state; NSString *city = [addressDictionary objectForKey:(NSString *)kABPersonAddressCityKey]; // 表达式 city = city == nil ? @"": city; textLabel.text = [NSString stringWithFormat:@"%@ \n%@ \n%@",state, address,city]; // textLabel.text=[NSString stringWithFormat:@"%@",state]; } }];</span>
// 小译:reverse 背面的反面的 completion:完成结束 handler:处理者,管理者 // reverse geocode requests // 小译:-()位置的反地理信息编码:某位置 完成处理的程序:处理 //- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
参数(CLGeocodeCompletionHandler)completionHandler 估计会让很多人困惑,我们来看看它的定义:
typedef void (^CLGeocodeCompletionHandler)(NSArray *placemarks, NSError *error);
这个定义了一个block类型,可以理解为一种函数的写法。这种声明结构如下回传值(^名字)(参数列);你需要做的就是给这个函数实现操作即可。最后看看效果
类似的,正向地里编码地理信息政编码
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
// 实现查询
[geocoder geocodeAddressString:textTextField.text completionHandler:^(NSArray *placemarks, NSError *error){
NSLog(@"查询记录%i",(int)[placemarks count]);
if ([placemarks count]>0) {
CLPlacemark *placemark=placemarks[0];
// 保存坐标对象
CLLocationCoordinate2D coordinate=placemark.location.coordinate;
NSString *strCoordinate=[NSString stringWithFormat:@"经度:%3.5f \n纬度:%3.5f",coordinate.latitude,coordinate.longitude];
// placemark 实力不俗
NSDictionary *addressDictionary=placemark.addressDictionary;
NSString *address=[addressDictionary objectForKey:(NSString*)kABPersonAddressStreetKey];
NSString *state=[addressDictionary objectForKey:(NSString*)kABPersonAddressStateKey];
NSString *city=[addressDictionary objectForKey:(NSString*)kABPersonAddressCityKey];
address=address==nil?@"":address;
state=state==nil?@"":state;
city=city==nil?@"":city;
textLabel.text=[NSString stringWithFormat:@"%@ \n%@ \n%@ \n%@",strCoordinate,state,city,address];
if ([address isEqualToString:@""]) {
NSLog(@"为什么没有街道信息");
}else
{
NSLog(@"%@",address);
}
[textTextField resignFirstResponder];
}
}];
相关代码:https://git.oschina.net/zhengaoxing/location1selflocation