MKMapView地理编码(即根据地址转换成坐标)

来源:互联网 发布:生日祝福源码 编辑:程序博客网 时间:2024/05/17 07:49

转载自:http://blog.csdn.net/ciwonderful/article/details/8395748

从iOS5之后MKReverseGeocoder成为了不推荐使用的类。因此有一个新的类取代了他的作用,那就是CLGeocoder类,使用该类进行反向解析也非常容易。

现在就CLGeocoder类来写一个地图定位并添加一个小标注。

1、首先,建立一个普通的view工程,添加CoreLocation.framework和MapKit.framework.导入头文件

2、然后定义一个MKMapView,和一个UIActivityIndicatorView指示器和一个findMe的方法,进行相应的@synthesize操作和release,实现<CLLocationManagerDelegate,MKMapViewDelegate>协议

[cpp] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2. #import <MapKit/MapKit.h>  
  3. #import <CoreLocation/CoreLocation.h>  
  4. @interface ViewController : UIViewController <CLLocationManagerDelegate,MKMapViewDelegate>  
  5. {  
  6.     MKMapView *mapView;  
  7.     UIActivityIndicatorView *indicator;  
  8. }  
  9. @property (nonatomic,retain) MKMapView *mapView;  
  10. @property (nonatomic,retain) UIActivityIndicatorView *indicator;  
  11. -(void)fineMe;  
  12. @end  
3、ViewDidLoad中加载控件,这样就可以显示地图咯,不过这样仅仅只能现实地图而已,没有什么实际的功能

[cpp] view plaincopy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     mapView=[[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 410)];  
  5.     mapView.mapType=MKMapTypeStandard;  
  6.     mapView.delegate=self;//实现代理方法  
  7.       
  8.     indicator=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(10, 420, 30, 30)];  
  9.     [indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];  
  10.     [indicator setHidesWhenStopped:YES];  
  11.     UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];  
  12.     button.frame=CGRectMake(230, 410, 80, 40);  
  13.     [button setTitle:@"FineMe" forState:UIControlStateNormal];  
  14.     [button addTarget:self action:@selector(fineMe) forControlEvents:UIControlEventTouchUpInside];  
  15.     [self.view addSubview:mapView];  
  16.     [self.view addSubview:indicator];  
  17.     [self.view addSubview:button];  
  18.     [self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]];  
  19.     // Do any additional setup after loading the view, typically from a nib.  
  20. }  
4、现在就要实现定义的那个Button的事件了,先初始化一个CLLocationManager对象,设置好代理以及一些定位服务的参数,最后启动start它。

让你的指示器转起来吧。。。。

[cpp] view plaincopy
  1. -(void)fineMe  
  2. {  
  3.     CLLocationManager *lm=[[CLLocationManager alloc]init];  
  4.     lm.delegate=self;  
  5.     lm.desiredAccuracy=kCLLocationAccuracyBest;  
  6.     [lm startUpdatingLocation];  
  7.     [indicator setHidden:NO];  
  8.     [indicator startAnimating];  
  9. }  
5、因为设置了CLLocationManager的代理,所以我们要去实现他的代理方法。这个方法就是下面的这个方法。

MKCoordinateRegion用来设置坐标显示范围,newLocation.coordinate是当前的位置,2000是范围为2公里,既2000米,单位是米,然后用mapView这个对象去设置它,这样你的屏幕就可以看到你当前的位置了,而且范围是2公里的,regionThatFits是用来调整长宽比例。

CLLocationManager用完了,我们就要nil它。在停止他的定位服务,不然在真机上是很耗电的。

CLGeocoder是IOS5才有的,替代了原来的MKReverseGeocoder,MKReverseGeocoder成为了不推荐使用的类,

先声明一个标注类对象MapLocation *annotaion=[[MapLocation alloc]init];这个在后面写它

再声明这个CLGeocoder来获得当前位置的地理信息,在Blocks中设置并获取街道的名称,

和设置当前的地理信息到标注类的对象中 annotaion.coordinate=[newLocation coordinate];

最后为mapView添加标注,这个时候也算是解析完了,也可以把指示器停了告诉用户您不用等待了。

PS:注释的地方是placemark的一个属性

[cpp] view plaincopy
  1. - (void)locationManager:(CLLocationManager *)manager  
  2.     didUpdateToLocation:(CLLocation *)newLocation  
  3.            fromLocation:(CLLocation *)oldLocation  
  4. {  
  5.     MKCoordinateRegion viewRegion=MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);  
  6.     MKCoordinateRegion adjustRegion=[mapView regionThatFits:viewRegion];  
  7.     [mapView setRegion:adjustRegion];  
  8.     manager.delegate=nil;  
  9.     [manager stopUpdatingLocation];  
  10.       
  11.       
  12.       
  13.     //MKReverseGeocoder *geocoder=[[MKReverseGeocoder   
  14.     MapLocation *annotaion=[[MapLocation alloc]init];  
  15.     CLGeocoder *geocoder = [[CLGeocoder alloc] init];  
  16.       
  17.     [geocoder  reverseGeocodeLocation: newLocation completionHandler:  
  18.      ^(NSArray *placemarks, NSError *error) {  
  19.          for (CLPlacemark *placemark in placemarks) {  
  20.              annotaion.streetAddress=placemark.name;  
  21.              annotaion.coordinate=[newLocation coordinate];  
  22.            //NSLog(@"name:%@\n country:%@\n postalCode:%@\n ISOcountryCode:%@\n ocean:%@\n inlandWater:%@\n locality:%@\n subLocality:%@\n administrativeArea:%@\n subAdministrativeArea:%@\n thoroughfare:%@\n subThoroughfare:%@\n",placemark.name,placemark.country,placemark.postalCode,placemark.ISOcountryCode,placemark.ocean,placemark.inlandWater,placemark.administrativeArea,placemark.subAdministrativeArea,placemark.locality,placemark.subLocality,placemark.thoroughfare,placemark.subThoroughfare);  
  23.          }  
  24.      }];  
  25.     [mapView addAnnotation:annotaion];  
  26.     [annotaion release];  
  27.     [indicator stopAnimating];  
  28. }  
6、MapLocation.h 标注类的代码

[cpp] view plaincopy
  1. #import <Foundation/Foundation.h>  
  2. #import <MapKit/MapKit.h>  
  3. @interface MapLocation : NSObject <MKAnnotation,NSCoding>  
  4. {  
  5.     NSString *streetAddress;  
  6.       
  7.     CLLocationCoordinate2D coordinate;  
  8. }  
  9. @property (nonatomic,copy) NSString *streetAddress;  
  10. @property (nonatomic,readwrite) CLLocationCoordinate2D coordinate;  
  11. @end  
MapLocation.m标注类的代码
[cpp] view plaincopy
  1. #import "MapLocation.h"  
  2. @implementation MapLocation  
  3. @synthesize streetAddress;  
  4. -(NSString *)title  
  5. {  
  6.     return @"您的位置!";  
  7. }  
  8. -(NSString *)subtitle  
  9. {  
  10.     NSMutableString *ret=[NSMutableString string];  
  11.     if (streetAddress)   
  12.         [ret appendString:streetAddress];  
  13.     return ret;  
  14. }  
  15. - (void)encodeWithCoder:(NSCoder *)aCoder  
  16. {  
  17.     [aCoder encodeObject:self.streetAddress forKey:@"streetAddress"];  
  18. }  
  19. - (id)initWithCoder:(NSCoder *)aDecoder  
  20. {  
  21.     self=[super init];  
  22.     if (self) {  
  23.         self.streetAddress=[aDecoder decodeObjectForKey:@"streetAddress"];  
  24.     }  
  25.     return self;  
  26. }  
  27. - (void)dealloc  
  28. {  
  29.     [streetAddress release];  
  30.     [super dealloc];  
  31. }  
  32. @end  

7、前面已经添加了标注 [mapView addAnnotation:annotaion];
那就来实现它的代理方法了,让他现实在地图上。

成功显示的情况

[cpp] view plaincopy
  1. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation  
  2. {  
  3.     MKPinAnnotationView *annotationView=(MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"];  
  4.       
  5.     if (annotationView==nil) {  
  6.         annotationView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"PIN_ANNOTATION"]autorelease];  
  7.     }  
  8.     annotationView.canShowCallout=YES;//有气泡显示  
  9.     annotationView.pinColor=MKPinAnnotationColorPurple;//图钉为紫色的  
  10.     annotationView.animatesDrop=YES;//动态得掉下来  
  11.     return annotationView;  
  12. }  
失败显示的情况,就alert提示一下吧

[cpp] view plaincopy
  1. - (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error  
  2. {  
  3.     UIAlertView *view=[[UIAlertView alloc]initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];  
  4.     [view show];  
  5.     [view release];  
  6. }  


总结:到这个,我们就可以实现功能了,点击button之后 就定位到你现在的位置,有一个小图钉标注你的位置,

点击图钉会有一个气泡显示出你当前的详细地址。

好咯,就先到这里咯,贴张效果图



源码下载地址:http://download.csdn.net/detail/ciwonderful/4920754


原创粉丝点击