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

[IOS地图开发系类]2、位置解码CLGeocoder

2015-01-04 21:59 519 查看


第一步的操作,获取到地址信息经纬度后,我们可以对其进行解码,解码采用的CLGeocoder这个类,使用方式如下:

1、在ViewControlelr.m文件中声明一个CLGeocoder的属性,给页面加一个button,然后在处理方法中,对上一篇获取的地址坐标进行解码,

code如下

#import"ViewController.h"
#import<CoreLocation/CoreLocation.h>
#import<MapKit/MapKit.h>
@interfaceViewController()<CLLocationManagerDelegate,MKMapViewDelegate>{
}
@property(nonatomic,retain)CLLocationManager*locationManager;
@property(nonatomic,retain)CLLocation*location;
@property(nonatomic,retain)CLGeocoder*myGeocoder;
@end

@implementationViewController

-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil
{
if(self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil]){
_locationManager=[[CLLocationManageralloc]init];
}
returnself;
}
-(void)dealloc
{
self.locationManager=nil;
self.location=nil;
self.myGeocoder=nil;
[superdealloc];
}
-(void)viewDidLoad
{
[superviewDidLoad];
//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.
//delegate
self.locationManager.delegate=self;
//Thedesiredlocationaccuracy.
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
//Specifiestheminimumupdatedistanceinmeters.
self.locationManager.distanceFilter=kCLDistanceFilterNone;
self.locationManager.purpose=
@"Toprovidefunctionalitybasedonuser'scurrentlocation.";
[self.locationManagerstartUpdatingLocation];

UIButton*addBt=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
addBt.frame=CGRectMake(0,00,320,50);
[addBtsetTitle:@"locationMe"forState:UIControlStateNormal];

[addBtaddTarget:selfaction:@selector(handleLocationMe:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:addBt];

self.myGeocoder=[[CLGeocoderalloc]init];
}

-(void)locationManager:(CLLocationManager*)managerdidChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSLog(@"didChangeAuthorizationStatus---%u",status);
}

-(void)locationManager:(CLLocationManager*)managerdidFailWithError:(NSError*)error
{
NSLog(@"didChangeAuthorizationStatus----%@",error);
}

-(void)locationManager:(CLLocationManager*)managerdidUpdateToLocation:(CLLocation*)newLocationfromLocation:(CLLocation*)oldLocation
{

UIAlertView*av=[[UIAlertViewalloc]initWithTitle:@"update"message:[NSStringstringWithFormat:@"didUpdateToLocation:newLocation:%@old:%@",newLocation,oldLocation]delegate:nilcancelButtonTitle:@"cancel"otherButtonTitles:@"ok",nil];
[avshow];
[avrelease];

self.location=newLocation;

NSLog(@"didUpdateToLocation:newLocation:%@",newLocation);
}

-(void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
}

-(IBAction)handleLocationMe:(id)sender
{

[self.myGeocoder
reverseGeocodeLocation:self.locationcompletionHandler:^(NSArray*placemarks,NSError*error){
if(error==nil&&[placemarkscount]>0){
CLPlacemark*placemark=[placemarksobjectAtIndex:0];
/*Wereceivedtheresults*/
NSLog(@"Country=%@",placemark.country);
NSLog(@"PostalCode=%@",placemark.postalCode);
NSLog(@"Locality=%@",placemark.locality);
NSLog(@"dic=%@",placemark.addressDictionary);
NSLog(@"dicFormattedAddressLines=%@",[placemark.addressDictionaryobjectForKey:@"FormattedAddressLines"]);
NSLog(@"dicName=%@",[placemark.addressDictionaryobjectForKey:@"Name"]);
NSLog(@"dicState=%@",[placemark.addressDictionaryobjectForKey:@"State"]);
NSLog(@"dicStreet=%@",[placemark.addressDictionaryobjectForKey:@"Street"]);
NSLog(@"dicSubLocality=%@",[placemark.addressDictionaryobjectForKey:@"SubLocality"]);
NSLog(@"dicSubThoroughfare=%@",[placemark.addressDictionaryobjectForKey:@"SubThoroughfare"]);
NSLog(@"dicThoroughfare=%@",[placemark.addressDictionaryobjectForKey:@"Thoroughfare"]);

}
elseif(error==nil&&
[placemarkscount]==0){
NSLog(@"Noresultswerereturned.");
}
elseif(error!=nil){
NSLog(@"Anerroroccurred=%@",error);
}
}];
}
@end


输出如下:

  

2013-07-2820:47:16.578LBS_001_CLLocationManager[30123:907]didChangeAuthorizationStatus---3
2013-07-2820:47:17.288LBS_001_CLLocationManager[30123:907]didUpdateToLocation:newLocation:<+39.93751238,+116.34143052>+/-65.00m(speed-1.00mps/course-1.00)@13-7-28中国标准时间下午8时47分16秒
2013-07-2820:47:17.300LBS_001_CLLocationManager[30123:907]didUpdateToLocation:newLocation:<+39.93751238,+116.34143052>+/-65.00m(speed-1.00mps/course-1.00)@13-7-28中国标准时间下午8时47分16秒
2013-07-2820:47:17.351LBS_001_CLLocationManager[30123:907]didUpdateToLocation:newLocation:<+39.93770077,+116.34199932>+/-70.88m(speed-1.00mps/course-1.00)@13-7-28中国标准时间下午8时47分16秒
2013-07-2820:47:17.392LBS_001_CLLocationManager[30123:907]didUpdateToLocation:newLocation:<+39.93767287,+116.34189085>+/-65.00m(speed-1.00mps/course-1.00)@13-7-28中国标准时间下午8时47分17秒
2013-07-2820:47:51.976LBS_001_CLLocationManager[30123:907]Country=中国
2013-07-2820:47:51.977LBS_001_CLLocationManager[30123:907]PostalCode=(null)
2013-07-2820:47:51.979LBS_001_CLLocationManager[30123:907]Locality=(null)
2013-07-2820:47:51.980LBS_001_CLLocationManager[30123:907]dic={
Country="\U4e2d\U56fd";
CountryCode=CN;
FormattedAddressLines=(
"\U4e2d\U56fd\U5317\U4eac\U5e02\U897f\U57ce\U533a\U5c55\U89c8\U8def\U8857\U9053\U897f\U76f4\U95e8\U5916\U5927\U8857132\U53f7"
);
Name="\U4e2d\U56fd\U5317\U4eac\U5e02\U897f\U57ce\U533a\U5c55\U89c8\U8def\U8857\U9053\U897f\U76f4\U95e8\U5916\U5927\U8857132\U53f7";
State="\U5317\U4eac\U5e02";
Street="\U897f\U76f4\U95e8\U5916\U5927\U8857132\U53f7";
SubLocality="\U897f\U57ce\U533a";
SubThoroughfare="132\U53f7";
Thoroughfare="\U897f\U76f4\U95e8\U5916\U5927\U8857";
}
2013-07-2820:47:51.982LBS_001_CLLocationManager[30123:907]dicFormattedAddressLines=(
"\U4e2d\U56fd\U5317\U4eac\U5e02\U897f\U57ce\U533a\U5c55\U89c8\U8def\U8857\U9053\U897f\U76f4\U95e8\U5916\U5927\U8857132\U53f7"
)
2013-07-2820:47:51.983LBS_001_CLLocationManager[30123:907]dicName=中国北京市西城区展览路街道西直门外大街132号
2013-07-2820:47:51.985LBS_001_CLLocationManager[30123:907]dicState=北京市
2013-07-2820:47:51.986LBS_001_CLLocationManager[30123:907]dicStreet=西直门外大街132号
2013-07-2820:47:51.987LBS_001_CLLocationManager[30123:907]dicSubLocality=西城区
2013-07-2820:47:51.988LBS_001_CLLocationManager[30123:907]dicSubThoroughfare=132号
2013-07-2820:47:51.989LBS_001_CLLocationManager[30123:907]dicThoroughfare=西直门外大街
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: