转载自: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>协议
- #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;
- @end
- - (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.
- }
让你的指示器转起来吧。。。。
- -(void)fineMe
- {
- CLLocationManager *lm=[[CLLocationManager alloc]init];
- lm.delegate=self;
- lm.desiredAccuracy=kCLLocationAccuracyBest;
- [lm startUpdatingLocation];
- [indicator setHidden:NO];
- [indicator startAnimating];
- }
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];
- }
- #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;
- @end
- #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
7、前面已经添加了标注 [mapView addAnnotation:annotaion];
那就来实现它的代理方法了,让他现实在地图上。
成功显示的情况
- - (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;
- }
- - (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];
- }
总结:到这个,我们就可以实现功能了,点击button之后 就定位到你现在的位置,有一个小图钉标注你的位置,
点击图钉会有一个气泡显示出你当前的详细地址。
好咯,就先到这里咯,贴张效果图
源码下载地址:http://download.csdn.net/detail/ciwonderful/4920754