如何添加大頭針(地標):框架
@interface MyAnnotation : NSObject <MKAnnotation>atom
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;spa
@property (nonatomic, copy) NSString *title;代理
@property (nonatomic, copy) NSString *subtitle;code
@end對象
<2>添加Annotation:blog
MyAnnotation *anno = [[MyAnnotation alloc] init];隊列
anno.title = @「中國";事件
anno.subtitle = @「北京」;圖片
//經度和緯度
anno.coordinate = CLLocationCoordinate2DMake(40, 110);
//添加大頭針到地圖中
[_mapView addAnnotation:anno];
// 讓地圖挪動到對應的位置(經緯度交叉處)
[_mapView setCenterCoordinate:anno.coordinate animated:YES];
自定義大頭針:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *ID = @"anno";
MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (annoView == nil) {
annoView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
// 顯示氣泡
annoView.canShowCallout = YES;
// 設置綠色
annoView.pinColor = MKPinAnnotationColorGreen;
}
return annoView;
}
//大頭針標註顏色枚舉
typedef NS_ENUM(NSUInteger, MKPinAnnotationColor) {
MKPinAnnotationColorRed = 0,
MKPinAnnotationColorGreen,
MKPinAnnotationColorPurple
} ;
//大頭針標註視圖類
@interface MKPinAnnotationView : MKAnnotationView
@property (nonatomic) MKPinAnnotationColor pinColor; //大頭針標註顏色
@property (nonatomic) BOOL animatesDrop; //是否顯示水滴動態
@end
//標註視圖類
@interface MKAnnotationView : NSView
- (instancetype)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString*)reuseIdentifier; //初始化
@property (nonatomic, readonly) NSString *reuseIdentifier; //重用標示符
@property (nonatomic, strong) id <MKAnnotation> annotation; //標註
@property (nonatomic, strong) UIImage *image; //圖像
@property (nonatomic) BOOL canShowCallout; //是否顯示氣泡
@property (strong, nonatomic) UIView *leftCalloutAccessoryView; //氣泡左視圖(多用來顯示圖片)
@property (strong, nonatomic) UIView *rightCalloutAccessoryView; //氣泡右視圖(多用來顯示圖片)
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MyAnnotation : NSObject<MKAnnotation> @property (assign,nonatomic)CLLocationCoordinate2D coordinate; //經緯度座標 @property (copy,nonatomic)NSString *title; //大頭針標註標題 @property (copy,nonatomic)NSString *subtitle; //大頭針標註子標題 @end
#import "ViewController.h" #import <MapKit/MapKit.h> #import "MyAnnotation.h" @interface ViewController ()<MKMapViewDelegate> @property (strong,nonatomic)MKMapView *mapView; //聲明地圖視圖控件 @end
- (void)viewDidLoad { [super viewDidLoad]; //建立Map實例 self.mapView = [[MKMapView alloc]initWithFrame:self.view.frame]; //設置地圖的類型 self.mapView.mapType = MKMapTypeStandard; //設置地圖的代理 self.mapView.delegate = self; //將地圖視圖添加到控制器視圖中 [self.view addSubview:self.mapView]; //建立一個標註 MyAnnotation *annotation = [[MyAnnotation alloc]init];
//設置北京的經緯度 annotation.coordinate = CLLocationCoordinate2DMake(40, 110); annotation.title = @"中國"; annotation.subtitle = @"北京"; //添加標註 [self.mapView addAnnotation:annotation]; //讓地圖顯示標註的區域 [self.mapView setCenterCoordinate:annotation.coordinate animated:YES]; //添加一個長按longPress手勢 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)]; [self.mapView addGestureRecognizer:longPress]; }
四、處理長按手勢事件,建立自定義的大頭針標註
-(void)longPress:(UILongPressGestureRecognizer *)sender { //獲取當前位置 CGPoint location = [sender locationInView:self.view]; //經緯度 CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView]; //建立新的標註 MyAnnotation *annotation = [[MyAnnotation alloc]init]; annotation.coordinate = coordinate; annotation.title = @"新標註"; annotation.subtitle = @"待開發..."; //添加標註 [self.mapView addAnnotation:annotation]; }
五、實現地圖視圖的代理方法
#pragma mark -mapView的代理方法
//顯示標註和睦泡,並在氣泡上設置圖片
#pragma mark 顯示標註視圖 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { //設置重用標示符 static NSString *annotationID = @"annotation"; //先從重用的隊列中找 MKPinAnnotationView *view = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID]; //沒找到就建立 if (!view) { view = [[MKPinAnnotationView alloc]init]; } //設置屬性 view.annotation = annotation; view.canShowCallout = YES;//顯示氣泡 view.pinColor = MKPinAnnotationColorGreen;//大頭針顏色 //顯示圖片,取代大頭針 //view.image = [UIImage imageNamed:@"1.png"]; //在氣泡視圖中顯示圖片 view.rightCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]]; return view; }
//選中標註和取消標註時調用的方法
#pragma mark 選中了標注的處理事件 -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { NSLog(@"選中了標注"); } #pragma mark 取消選中標註的處理事件 -(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view { NSLog(@"取消了標註"); }
演示結果:(選中和取消標註時,輸出結果就不打印了)
開始時: 點擊大頭針標註時:
再隨意在某一個地方長按時出現一個新的大頭這標註 點擊大頭標註時: