地图标注常见于应用当中,比如常见情景:
1、地图应用当中当你需要点击收藏某个地点时,就相当于把这个坐标信息标注出来。
2、地图定位中的小蓝点,相当于在地图上的一个标注。
实现地图上的标注通常有以下几步:
1、新建一个实现了MKAnnotation协议的类。
主要去实现其中的CLLocationCoordinate2D、title、subtitle三个属性方法;
2、创建MKAnnotation对象,并初始化其中的CLLocationCoordinate2D、title、subtitle属性。
3、新建MKMapView并设置显示坐标、缩放级别及其代理方法。添加标注时候,注意可以添加单个标注也可以添加多个标注,删除标注方法也类似。
[_mapViewremoveAnnotations:_mapView.annotations];
[_mapView addAnnotations:_annotations];
4、当调用添加标注方法时,调用mapView的代理方法显示标注详情:
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
添加标注。
注意其中几点:
a、重用机制,主要用来减少对象的重复创建。
首先先从某个容器去看有没有没用的对象,有就使用,只需要替换其中的值,没有则新创建对象
b、如果是大头针有两个设置需要注意:
pinAnotationView.animatesDrop = YES; 表示显示标注时,出现动画
pinAnotationView.canShowCallout = YES;点击显示详情。
c、点击后的视图有左右两个视图设置。
leftCalloutAccessoryView
rightCalloutAccessoryView
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface LAnnotation :NSObject<MKAnnotation>
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *subtitle;
@property (nonatomic,assign)NSInteger index;
-(id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
title:(NSString *)title
subtitle:(NSString *)subtitle
index:(NSInteger) index;
@end
#import "LAnnotation.h"
@implementation LAnnotation
-(id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
title:(NSString *)title
subtitle:(NSString *)subtitle
index:(NSInteger)index{
self= [superinit];
if (self) {
self.coordinate = coordinate;
self.title = title;
self.subtitle = subtitle;
self.index = index;
}
return self;
}
@end
_mapView = [[MKMapViewalloc]initWithFrame:CGRectMake(0,0, width, height-40)];
CLLocationCoordinate2D location = {31.19316,121.43154};
MKCoordinateSpan span ={0.05,0.05};
MKCoordinateRegion region = {location,span};
[ _mapView setRegion:region];
_mapView.delegate =self;
[self.viewaddSubview:_mapView];
CLLocationCoordinate2D cordination1= {31.196,121.433};
_annotations = [[NSMutableArrayalloc]initWithCapacity:3];
LAnnotation *annotation1 = [[LAnnotationalloc]initWithCoordinate:cordination1title:@"中心路"subtitle:@"光源西擦"index:0];
[_annotations addObject:annotation1];
CLLocationCoordinate2D cordination2= {31.197,121.434};
LAnnotation *annotation2 = [[LAnnotationalloc]initWithCoordinate:cordination2title:@"中心dd路"subtitle:@"光源西擦dd"index:1];
CLLocationCoordinate2D cordination3= {31.198,121.435};
LAnnotation *annotation3 = [[LAnnotationalloc]initWithCoordinate:cordination3title:@"中擦路"subtitle:@"光钱财"index:2];
[_annotations addObject:annotation2];
[_annotations addObject:annotation3];
-(void)flagButton:(id)sender{
[_mapViewremoveAnnotations:_mapView.annotations];
[_mapView addAnnotations:_annotations];
}
#pragma mark -MKMapViewDelegate
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
if (![[annotation class]isSubclassOfClass:[LAnnotationclass]]) {
return nil;
}
LAnnotation *blAnnotation = (LAnnotation *)annotation;
static NSString *annotationIdentifier =@"reuserIdentifier";
MKPinAnnotationView *pinAnotationView = (MKPinAnnotationView *)[mapViewdequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (pinAnotationView==nil) {
pinAnotationView = [[MKPinAnnotationViewalloc]initWithAnnotation:blAnnotationreuseIdentifier:annotationIdentifier];
pinAnotationView.animatesDrop = YES;
pinAnotationView.canShowCallout = YES;
UIButton *leftButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0, 30, 30)];
leftButton.backgroundColor = [UIColorredColor];
pinAnotationView.leftCalloutAccessoryView = leftButton;
UIButton *rightButton = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];
pinAnotationView.rightCalloutAccessoryView = rightButton;
}
if (blAnnotation.index ==0) {
pinAnotationView.pinColor = MKPinAnnotationColorGreen;
pinAnotationView.leftCalloutAccessoryView.tag =0;
}else if (blAnnotation.index ==1) {
pinAnotationView.pinColor = MKPinAnnotationColorPurple;
}else if (blAnnotation.index ==2) {
pinAnotationView.pinColor = MKPinAnnotationColorRed;
}
pinAnotationView.tag = blAnnotation.index;
return pinAnotationView;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
switch (view.tag) {
case 0:
NSLog(@"第一个");
break;
default:
NSLog(@"第其它个");
break;
}
}