自定义showCallout 左右view
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ MKAnnotationView *aView=[mapView dequeueReusableAnnotationViewWithIdentifier:@"testMapView"]; if(!aView){ aView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"testMapView"]; //这里是用的MKPinAnnotationView 初始化 当然也可以用MKAnnotationView 只是用MKAnnotationView就必须对MKAnnotationView对象.image赋值 否则 没有图钉显示标记位置 aView.canShowCallout=YES; //设置是否显示ShowCallout } aView.annotation=annotation; UIImageView *imageV=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)]; //0,0,30,30是组不错的数据。 imageV.image=[UIImage imageNamed:@"Default.png"]; aView.leftCalloutAccessoryView=imageV; // aView.image=[UIImage imageNamed:@"Default.png"]; // aView.image用于修改在地图上标记的图钉 return aView; } 实现MKMapViewDelegate协议 并设置当前mapView.delegate
关于annotation
MyAnnotation 用于作为放入MapView annotation数组的对象[放入此数组的id需旅行 <MKAnnotation>协议] 此协议是 readOnly的几个属性的"get"方法实现。因为readOnly 所以无法对其赋值。故传入一个“辞典” 然后在对应的“get”方法中去 词典中去相应的数据。 MyAnnotation.h #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MyAnnotation : NSObject<MKAnnotation> +(MyAnnotation*)initWithNSMutableDictionary:(NSMutableDictionary *)ns; @end MyAnnotation.m #import "MyAnnotation.h" @interface MyAnnotation() @property NSMutableDictionary *save; @end @implementation MyAnnotation @synthesize save=_save; + (MyAnnotation *)initWithNSMutableDictionary:(NSMutableDictionary *)ns{ MyAnnotation *ma=[[MyAnnotation alloc]init]; ma.save=ns; return ma; } #pragma mark - <Annotation> -(NSString *)title{ return [self.save objectForKey:@"title"]; } -(NSString *)subtitle{ return [self.save objectForKey:@"subTitle"]; } -(CLLocationCoordinate2D)coordinate{ CLLocationCoordinate2D result; result.latitude=20.0;//纬度 result.longitude=30.0;//经度 return result; } @end
VC相关
当mapView或annotations 被设置时则执行更新。刷新界面 保证数据同步展示在屏幕。 #import "ViewController.h" #import <MapKit/MapKit.h> #import "MyAnnotation.h" @interface ViewController () @property (weak, nonatomic) IBOutlet MKMapView *myMapView; @property (strong,nonatomic) NSArray *annotations; @end @implementation ViewController @synthesize myMapView=_myMapView; @synthesize annotations=_annotations; -(void)setAnnotations:(NSArray *)annotations{ if(_annotations!=annotations){ _annotations=annotations; [self update]; } } -(void)setMyMapView:(MKMapView *)myMapView{ if(_myMapView!=myMapView){ _myMapView=myMapView; [self update]; } } - (void)update{ if(self.myMapView.annotations){ [self.myMapView removeAnnotations:self.myMapView.annotations]; } if(self.annotations){ [self.myMapView addAnnotations:self.annotations]; } } - (void)viewDidLoad { [super viewDidLoad]; NSMutableDictionary *nsTemp=[[NSMutableDictionary alloc]init]; [nsTemp setObject:@"poolo's title" forKey:@"title"]; [nsTemp setObject:@"poolo's SubTitle" forKey:@"subTitle"]; self.annotations=@[[MyAnnotation initWithNSMutableDictionary:nsTemp]]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end