大頭針(相當于TableView的model)(MKAnnotation)
在iOS開發(fā)中經常會標記某個位置,需要使用地圖標注,也就是大家俗稱的“大頭針”。只要一個NSObject類實現(xiàn)MKAnnotation協(xié)議就可以作為一個大頭針,通常會重寫協(xié)議中coordinate(標記位置)、title(標題)、subtitle(子標題)三個屬性,然后在程序中創(chuàng)建大頭針對象并調用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
設置大頭針視圖(TableView的cell)(MKAnnotationView)
在一些應用中系統(tǒng)默認的大頭針樣式可能無法滿足實際的需求,此時就需要修改大頭針視圖默認樣式。根據(jù)前面MapKit的代理方法不難發(fā)現(xiàn)- (MKAnnotationView *)mapView:(MKMapView * )mapView viewForAnnotation:(id <MKAnnotation>)annotation;方法可以返回一個大頭針視圖,只要實現(xiàn)這個方法并在這個方法中定義一個大頭針視圖MKAnnotationView對象并設置相關屬性就可以改變默認大頭針的樣式. (可以像cell一樣自定義) 新建一個類繼承自MKAnnotation,然后重寫此方法 :
- (instancetype)initWithAnnotation:(nullable id <MKAnnotation>)annotation reuseIdentifier:(nullable NSString *)reuseIdentifier
MKAnnotationView的常用屬性
- annotation 大頭針模型信息,包括標題、子標題、地理位置。
- image 大頭針圖片
- canShowCallout 點擊大頭針是否顯示標題、子標題內容等,
- calloutOffset 點擊大頭針時彈出詳情信息視圖的偏移量
- selected 是否被選中狀態(tài)
- leftCalloutAccessoryView 彈出詳情左側視圖
#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
{
//由于當前位置的標注也是一個大頭針,所以此時需要判斷,此代理方法返回nil使用默認大頭針視圖
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; //允許交互點擊
annotationView.calloutOffset = CGPointMake(0, 0); //定義詳情視圖偏移量
annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
}
annotationView.annotation = annotation;
annotationView.image = [UIImage imageNamed:@"勾選"]; //設置大頭針視圖的圖片
return annotationView;
}else{
return nil;
}
}