大頭針(相當(dāng)于TableView的model)(MKAnnotation)
在iOS開發(fā)中經(jīng)常會(huì)標(biāo)記某個(gè)位置,需要使用地圖標(biāo)注,也就是大家俗稱的“大頭針”。只要一個(gè)NSObject類實(shí)現(xiàn)MKAnnotation協(xié)議就可以作為一個(gè)大頭針,通常會(huì)重寫協(xié)議中coordinate(標(biāo)記位置)、title(標(biāo)題)、subtitle(子標(biāo)題)三個(gè)屬性,然后在程序中創(chuàng)建大頭針對(duì)象并調(diào)用addAnnotation:方法添加大頭針即可
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString * title;
@property (nonatomic,copy) NSString * subtitle;
@end
設(shè)置大頭針視圖(TableView的cell)(MKAnnotationView)
在一些應(yīng)用中系統(tǒng)默認(rèn)的大頭針樣式可能無(wú)法滿足實(shí)際的需求,此時(shí)就需要修改大頭針視圖默認(rèn)樣式。根據(jù)前面MapKit的代理方法不難發(fā)現(xiàn)- (MKAnnotationView *)mapView:(MKMapView * )mapView viewForAnnotation:(id <MKAnnotation>)annotation;方法可以返回一個(gè)大頭針視圖,只要實(shí)現(xiàn)這個(gè)方法并在這個(gè)方法中定義一個(gè)大頭針視圖MKAnnotationView對(duì)象并設(shè)置相關(guān)屬性就可以改變默認(rèn)大頭針的樣式. (可以像cell一樣自定義) 新建一個(gè)類繼承自MKAnnotation,然后重寫此方法 :
- (instancetype)initWithAnnotation:(nullable id <MKAnnotation>)annotation reuseIdentifier:(nullable NSString *)reuseIdentifier
MKAnnotationView的常用屬性
- annotation 大頭針模型信息,包括標(biāo)題、子標(biāo)題、地理位置。
- image 大頭針圖片
- canShowCallout 點(diǎn)擊大頭針是否顯示標(biāo)題、子標(biāo)題內(nèi)容等,
- calloutOffset 點(diǎn)擊大頭針時(shí)彈出詳情信息視圖的偏移量
- selected 是否被選中狀態(tài)
- leftCalloutAccessoryView 彈出詳情左側(cè)視圖
#import "FistViewController.h"
#import <MapKit/MapKit.h>
#import "MyAnnotation.h"
@interface FistViewController ()<MKMapViewDelegate>
@end
@implementation FistViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
MKMapView * _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
_mapView.mapType = MKMapTypeStandard;
_mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
_mapView.delegate = self;
CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(39.95, 116.35);
MyAnnotation *annotation1=[[MyAnnotation alloc]init];
annotation1.title=@"CMJ Studio";
annotation1.subtitle=@"Kenshin Cui's Studios";
annotation1.coordinate=location1;
[_mapView addAnnotation:annotation1];
CLLocationCoordinate2D location2=CLLocationCoordinate2DMake(38.95, 116.35);
MyAnnotation *annotation2=[[MyAnnotation alloc]init];
annotation2.title=@" Studio";
annotation2.subtitle=@"Kenshin Cui's Studios";
annotation2.coordinate=location2;
[_mapView addAnnotation:annotation2];
[self.view addSubview:_mapView];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
//由于當(dāng)前位置的標(biāo)注也是一個(gè)大頭針,所以此時(shí)需要判斷,此代理方法返回nil使用默認(rèn)大頭針視圖
if ([annotation isKindOfClass:[MyAnnotation class]]) {
static NSString * key1 = @"Annotation";
MKAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:key1];
if (!annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:key1];
annotationView.canShowCallout = true; //允許交互點(diǎn)擊
annotationView.calloutOffset = CGPointMake(0, 0); //定義詳情視圖偏移量
annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
}
annotationView.annotation = annotation;
annotationView.image = [UIImage imageNamed:@"勾選"]; //設(shè)置大頭針視圖的圖片
return annotationView;
}else{
return nil;
}
}