ios 百度地图替换地图显示多种大头针

上网看了好多添加多种大头针的demo都是错的
下面介绍两种添加大头针方法

方法一

1,写一个分类继承BMKPointAnnotation
@interface BXMKPointAnnotation : BMKPointAnnotation

@property (copy) NSString *title1;
@property (copy) NSString *title2;

@end

这样就可以使用了
先清除之前的大头针
    [_mapView removeAnnotations:_mapView.annotations];
把大头针添加倒数组中
  _carMutAry = [[NSMutableArray alloc]init];
 for (NSInteger i = 0; i < 10; i++)
    {
        double lat =  (arc4random() % 100) * 0.001f;
        double lon =  (arc4random() % 100) * 0.001f;
        BXMKPointAnnotation *point = [[BXMKPointAnnotation alloc] init];
        point.title = @"西安市";
        point.subtitle = @"我在这";
        point.title1 = @"自定义泡泡测试";
        point.title2 = @"自定义测试";
        point.coordinate = CLLocationCoordinate2DMake(tt.latitude + lat, tt.longitude + lon);
        [_carMutAry addObject:point];
        
    }
放到地图上
    [_mapView addAnnotations:_carMutAry];

#pragma mark 设置大头针
#pragma mark 设置大头针
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation
{
//判断你添加的那个大头针随便else if
    if([annotation isKindOfClass:[BXMKPointAnnotation class]])
    {
    BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
    newAnnotationView.pinColor = BMKPinAnnotationColorRed;
    newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
    newAnnotationView.draggable = NO;
    newAnnotationView.annotation=annotation;
    //        newAnnotationView.image = [UIImage imageNamed:@"bx.jpg"];   //把大头针换成别的图片
    return newAnnotationView;
    }
    return nil;

}
方法二
继承BMKAnnotationView
PPXAnnotationView.h
#import 
@interface PPXAnnotationView : BMKAnnotationView
/**
 *  创建方法
 *
 *  @param mapView 地图
 *
 *  @return 大头针
 */
+ (instancetype)annotationViewWithMap:(BMKMapView *)mapView withAnnotation:(id )annotation;

@end

PPXAnnotationView.m
#import "PPXAnnotationView.h"
#import 
#import "PPXPointAnnotation.h"


@implementation PPXAnnotationView

- (instancetype)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
    }
    return self;
}

+ (instancetype)annotationViewWithMap:(BMKMapView *)mapView withAnnotation:(id )annotation {
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        static NSString *identifier = @"annotation";
        // 1.从缓存池中取
        PPXAnnotationView *annoView = (PPXAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        // 2.如果缓存池中没有, 创建一个新的
        if (annoView == nil) {
            annoView = [[PPXAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        }
        if ([annotation isKindOfClass:[PPXPointAnnotation class]]) {
            annoView.annotation = (PPXPointAnnotation *)annotation;
        }
        annoView.image = [UIImage imageNamed:@"BX_icon_green"];
        return annoView;
    }
    return nil;
}

@end

PPXPointAnnotation.h
#import 
//你的数据模型随便写啦
@class PPXPoi;

@interface PPXPointAnnotation : BMKPointAnnotation


/** PPXPoi*/
@property (nonatomic, strong) PPXPoi *poi;
/** 标注点的protocol,提供了标注类的基本信息函数*/
@property (nonatomic, weak) id delegate;

@end

.m里面没啥就不说了
这样就可以用了
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:@"西单" forKey:@"areaName"];
    [dic setValue:@"子标题" forKey:@"name"];
    
    NSMutableArray *ary = [[NSMutableArray alloc]initWithObjects:dic,dic,dic,dic, nil];
    for (NSInteger i = 0; i < ary.count; i++)
    {
        PPXPoi *poi = [PPXPoi mj_objectWithKeyValues:ary[i]];
        double lat =  (arc4random() % 100) * 0.001f;
        double lon =  (arc4random() % 100) * 0.001f;
        PPXPointAnnotation *annotation = [[PPXPointAnnotation alloc] init];
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(tt.latitude + lat, tt.longitude + lon);
        annotation.coordinate = coordinate;
        annotation.poi = poi;
        [_mapView addAnnotation:annotation];
    }
#pragma mark 设置大头针
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation
{
    PPXAnnotationView *annotationView = [PPXAnnotationView annotationViewWithMap:mapView withAnnotation:annotation];
    PPXPaopaoView *paopaoView = [[PPXPaopaoView alloc] init];
    paopaoView.delegate = self;
    PPXPointAnnotation *anno = (PPXPointAnnotation *)annotationView.annotation;
    paopaoView.poi = anno.poi;
    return annotationView;

}

完成谢谢大家支持

你可能感兴趣的:(ios 百度地图替换地图显示多种大头针)