1、加载基本地图视图展示
2、创建一个大头针模型(主要是经纬度)涉及到的类有BMKPointAnnotation、BMKAnnotationView、BMKPinAnnotationView
BMKPointAnnotation:大头针模型,用来描述大头针的坐标等信息;
BMKAnnotationView:标注视图,包含标注上面的标题、子标题、及相关按钮等;
BMKPinAnnotationView:大头针视图,继承自BMKAnnotationView,可以理解为大头针是一种特殊的标注;
3、将大头针添加到地图,到此为止事实上已经搞定,地图上能正常显示刚刚所标注的大头针了,如下效果图所示:
4、此外,mapView还有另外两个代理方法可以对大头针进行后续的处理,比如大头针颜色,动画效果(从天而降)等
– (void)mapView:(BMKMapView *)mapView didAddAnnotationViews:(NSArray *)views
– (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id
5、关键代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #import "SAViewController.h" #import "BMapKit.h" @interface SAViewController () <BMKPoiSearchDelegate, BMKMapViewDelegate> @property (nonatomic, strong) BMKMapView *mapView; // 地图 @property (nonatomic, strong) BMKPointAnnotation *pointAnnotation; // 大头针 @end @implementation SAViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建一个地图添加到控制器视图上 self.mapView.frame = self.view.frame; [self.view addSubview:self.mapView]; // 创建一个大头针模型,设置大头针经纬坐标 self.pointAnnotation = [[BMKPointAnnotation alloc] init]; self.pointAnnotation.title = @"大头针"; self.pointAnnotation.subtitle = @"一个神奇的东西"; self.pointAnnotation.coordinate = (CLLocationCoordinate2D){39.9, 116.4}; [self.mapView addAnnotation:self.pointAnnotation]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.mapView viewWillAppear]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.mapView viewWillDisappear]; } #pragma mark mapView的Get方法,延时加载 - (BMKMapView *)mapView { if (!_mapView) { _mapView = [[BMKMapView alloc] init]; _mapView.delegate = self; } return _mapView; } #pragma mark 添加标注时调用 - (void)mapView:(BMKMapView *)mapView didAddAnnotationViews:(NSArray *)views { NSLog(@"地图添加标注时会调用!"); } #pragma mark 为标注生成新的视图 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation { NSLog(@"地图重新生成标注蒙层时调用!"); // 类似TableViewCell,可从缓存中重复利用的View BMKPinAnnotationView *annotationView; annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"Annotation"]; if (!annotationView) { // BMKPinAnnotationView是BMKAnnotationView的子类(大头针标注类) annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Annotation"]; // 设置大头针的颜色为绿色 annotationView.pinColor = BMKPinAnnotationColorGreen; } return annotationView; } @end |