从iOS5之后MKReverseGeocoder成为了不推荐使用的类。因此有一个新的类取代了他的作用,那就是CLGeocoder类,使用该类进行反向解析也非常容易。
现在就CLGeocoder类来写一个地图定位并添加一个小标注。
1、首先,建立一个普通的view工程,添加CoreLocation.framework和MapKit.framework.导入头文件
2、然后定义一个MKMapView,和一个UIActivityIndicatorView指示器和一个findMe的方法,进行相应的@synthesize操作和release,实现<CLLocationManagerDelegate,MKMapViewDelegate>协议
#import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController : UIViewController <CLLocationManagerDelegate,MKMapViewDelegate> { MKMapView *mapView; UIActivityIndicatorView *indicator; } @property (nonatomic,retain) MKMapView *mapView; @property (nonatomic,retain) UIActivityIndicatorView *indicator; -(void)fineMe; @end3、ViewDidLoad中加载控件,这样就可以显示地图咯,不过这样仅仅只能现实地图而已,没有什么实际的功能
- (void)viewDidLoad { [super viewDidLoad]; mapView=[[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 410)]; mapView.mapType=MKMapTypeStandard; mapView.delegate=self;//实现代理方法 indicator=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(10, 420, 30, 30)]; [indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray]; [indicator setHidesWhenStopped:YES]; UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame=CGRectMake(230, 410, 80, 40); [button setTitle:@"FineMe" forState:UIControlStateNormal]; [button addTarget:self action:@selector(fineMe) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:mapView]; [self.view addSubview:indicator]; [self.view addSubview:button]; [self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]]; // Do any additional setup after loading the view, typically from a nib. }4、现在就要实现定义的那个Button的事件了,先初始化一个CLLocationManager对象,设置好代理以及一些定位服务的参数,最后启动start它。
让你的指示器转起来吧。。。。
-(void)fineMe { CLLocationManager *lm=[[CLLocationManager alloc]init]; lm.delegate=self; lm.desiredAccuracy=kCLLocationAccuracyBest; [lm startUpdatingLocation]; [indicator setHidden:NO]; [indicator startAnimating]; }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的一个属性
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { MKCoordinateRegion viewRegion=MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000); MKCoordinateRegion adjustRegion=[mapView regionThatFits:viewRegion]; [mapView setRegion:adjustRegion]; manager.delegate=nil; [manager stopUpdatingLocation]; //MKReverseGeocoder *geocoder=[[MKReverseGeocoder MapLocation *annotaion=[[MapLocation alloc]init]; CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation: newLocation completionHandler: ^(NSArray *placemarks, NSError *error) { for (CLPlacemark *placemark in placemarks) { annotaion.streetAddress=placemark.name; annotaion.coordinate=[newLocation coordinate]; //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); } }]; [mapView addAnnotation:annotaion]; [annotaion release]; [indicator stopAnimating]; }6、MapLocation.h 标注类的代码
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MapLocation : NSObject <MKAnnotation,NSCoding> { NSString *streetAddress; CLLocationCoordinate2D coordinate; } @property (nonatomic,copy) NSString *streetAddress; @property (nonatomic,readwrite) CLLocationCoordinate2D coordinate; @endMapLocation.m标注类的代码
#import "MapLocation.h" @implementation MapLocation @synthesize streetAddress; -(NSString *)title { return @"您的位置!"; } -(NSString *)subtitle { NSMutableString *ret=[NSMutableString string]; if (streetAddress) [ret appendString:streetAddress]; return ret; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.streetAddress forKey:@"streetAddress"]; } - (id)initWithCoder:(NSCoder *)aDecoder { self=[super init]; if (self) { self.streetAddress=[aDecoder decodeObjectForKey:@"streetAddress"]; } return self; } - (void)dealloc { [streetAddress release]; [super dealloc]; } @end
成功显示的情况
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { MKPinAnnotationView *annotationView=(MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"]; if (annotationView==nil) { annotationView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"PIN_ANNOTATION"]autorelease]; } annotationView.canShowCallout=YES;//有气泡显示 annotationView.pinColor=MKPinAnnotationColorPurple;//图钉为紫色的 annotationView.animatesDrop=YES;//动态得掉下来 return annotationView; }失败显示的情况,就alert提示一下吧
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error { UIAlertView *view=[[UIAlertView alloc]initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [view show]; [view release]; }
点击图钉会有一个气泡显示出你当前的详细地址。
好咯,就先到这里咯,贴张效果图
源码下载地址:http://download.csdn.net/detail/ciwonderful/4920754