经常会在地图上看到标志出自己的位置,在iOS中,在地图上显示标注也比较简单,如果还不知道怎么使用MKMapView可以查看 之前的一篇博文 http://my.oschina.net/CarlHuang/blog/135589.
首先我们先自定义一个MyAnnotation类,继承自NSObject 和实现 <MKAnnotation> 协议 , 定义三个属性,设置为只读.
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy,readonly) NSString * title;
@property (nonatomic,copy,readonly) NSString * subtitle;
MyAnnotation.h 文件代码:
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MyAnnotation : NSObject <MKAnnotation> //显示标注的经纬度 @property (nonatomic,readonly) CLLocationCoordinate2D coordinate; //标注的标题 @property (nonatomic,copy,readonly) NSString * title; //标注的子标题 @property (nonatomic,copy,readonly) NSString * subtitle; @ -(id)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates title:(NSString *)paramTitle subTitle:(NSString *)paramTitle; @endMyAnnotation.m 文件代码:
#import "MyAnnotation.h" @implementation MyAnnotation -(id)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates title:(NSString *)paramTitle subTitle:(NSString *)paramSubitle { self = [super init]; if(self != nil) { _coordinate = paramCoordinates; _title = paramTitle; _subtitle = paramSubitle; _pinColor = MKPinAnnotationColorGreen; } return self; } @end在地图中添加标注的方法
[self.mapView addAnnotation:]
[self.mapView addAnnotations:arr];
添加标注会调用MKMapViewDelegate 中的
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 方法.
ViewController.h 文件代码:
#import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController : UIViewController @property (nonatomic,strong) MKMapView * myMapView; @endViewController.m文件代码:
#import "ViewController.h" #import "MyAnnotation.h" #import "MapAnnotation.h" @interface ViewController () <MKMapViewDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; self.myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; self.myMapView.mapType = MKMapTypeHybrid; self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.myMapView.delegate = self; // self.myMapView.showsUserLocation = YES; [self.view addSubview:self.myMapView]; CLLocationCoordinate2D location = CLLocationCoordinate2DMake(50.82191692907181, -0.13811767101287842); MyAnnotation * myAnnotation = [[MyAnnotation alloc] initWithCoordinates:location title:@"My Title" subTitle:@"My Sub Title"]; [self.myMapView addAnnotation:myAnnotation]; //自动显示标注的layout [self.myMapView selectAnnotation:myAnnotation animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return YES; } -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { MKAnnotationView * result = nil; if([annotation isKindOfClass:[MyAnnotation class]] == NO) { return result; } if([mapView isEqual:self.myMapView] == NO) { return result; } MyAnnotation * senderAnnotation = (MyAnnotation *)annotation; NSString * pinReusableIdentifier = [MyAnnotation reusableIdentifierForPinColor:senderAnnotation.pinColor]; MKPinAnnotationView * annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinReusableIdentifier]; if(annotationView == nil) { annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:senderAnnotation reuseIdentifier:pinReusableIdentifier]; [annotationView setCanShowCallout:YES]; } UIButton * button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; annotationView.rightCalloutAccessoryView = button; annotationView.opaque = NO; annotationView.animatesDrop = YES; annotationView.draggable = YES; annotationView.selected = YES; annotationView.calloutOffset = CGPointMake(15, 15); UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SFIcon.png"]]; annotationView.leftCalloutAccessoryView = imageView; result = annotationView; return result; } @end