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<BMKAnnotation>)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 <BaiduMapAPI_Map/BMKMapView.h>
@interface PPXAnnotationView : BMKAnnotationView
/**
 *  創建方法
 *
 *  @param mapView 地圖
 *
 *  @return 大頭針
 */
+ (instancetype)annotationViewWithMap:(BMKMapView *)mapView withAnnotation:(id <BMKAnnotation>)annotation;

@end

PPXAnnotationView.m
#import "PPXAnnotationView.h"
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
#import "PPXPointAnnotation.h"


@implementation PPXAnnotationView

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

+ (instancetype)annotationViewWithMap:(BMKMapView *)mapView withAnnotation:(id <BMKAnnotation>)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 <BaiduMapAPI_Map/BMKPointAnnotation.h>
//你的數據模型隨便寫啦
@class PPXPoi;

@interface PPXPointAnnotation : BMKPointAnnotation


/** PPXPoi*/
@property (nonatomic, strong) PPXPoi *poi;
/** 標注點的protocol,提供了標注類的基本信息函數*/
@property (nonatomic, weak) id<BMKAnnotation> 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<BMKAnnotation>)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;

}

完成謝謝大家支持

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容