demo 地址
MyAnnotation.h文件
#import <Foundation/Foundation.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
@interface MyAnnotation : BMKPointAnnotation
/**
* 圖標(biāo)
*/
@property (nonatomic, copy) NSString *icon;
@end
MyAnnotation.m文件
#import "MyAnnotation.h"
@implementation MyAnnotation
@end
MyAnnotationView.h文件
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Map/BMKAnnotationView.h>
@interface MyAnnotationView : BMKAnnotationView
/**
* 創(chuàng)建方法
*
* @param mapView 地圖
*
* @return 大頭針
*/
+ (instancetype)annotationViewWithMap:(BMKMapView *)mapView;
@end
MyAnnotationView.m文件
#import "MyAnnotationView.h"
#import "MyAnnotation.h"@implementation MyAnnotationView
- (instancetype)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
}
return self;
}
+ (instancetype)annotationViewWithMap:(BMKMapView *)mapView{
static NSString *identifier = @"anno";
// 從緩存池中取 MyAnnotationView *annoView = (HXAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
// 如果緩存池中沒(méi)有, 創(chuàng)建一個(gè)新的
if (annoView == nil) {
annoView = [[MyAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
}
return annoView;
}
- (void)setAnnotation:(MyAnnotation *)annotation{
[super setAnnotation:annotation];
//設(shè)置圖標(biāo)
self.image = [UIImage imageNamed:@"icon_green"];
}
@end
之后在控制器文件中,首先要先從服務(wù)器獲取數(shù)據(jù),取到數(shù)據(jù)后,根據(jù)經(jīng)緯度坐標(biāo),也就是一個(gè)個(gè)的Annotation顯示到地圖上.
//可以使用,anno是標(biāo)注
[_mapView addAnnotation:_mapView.annotations];
//或者使用,annos是標(biāo)注數(shù)組
[_mapView addAnnotations:_mapView.annotations];
之后在調(diào)用BMKMapViewDelegate的方法:
#pragma mark -BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation{
// 對(duì)用戶(hù)當(dāng)前的位置的大頭針特殊處理
if ([annotation isKindOfClass:[HXAnnotation class]] == NO) {
return nil;
}
// 創(chuàng)建大頭針
MyAnnotationView *annoView = [MyAnnotationView annotationViewWithMap:mapView];
// 設(shè)置模型
annoView.annotation = annotation;
self.anno = annotation;
//初始化泡泡視圖
DetailsView *detailView = [[[NSBundle mainBundle]loadNibNamed:@"HXDetailsView" owner:nil options:nil] lastObject];
//顯示到paopaoView上
annoView.paopaoView = [[BMKActionPaopaoView alloc] initWithCustomView:detailView];
// 返回大頭針
return annoView;
}
才能將annoView顯示到地圖上.
如果是自定義的paopaoView,BMKMapViewDelegate會(huì)有如下方法:
-(void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)
我在開(kāi)發(fā)過(guò)程中,調(diào)用上面的方法并沒(méi)有作用,所以我在自定義的paopaoView里添加了一個(gè)按鈕,蓋滿(mǎn)整個(gè)paopaoView,之后添加一個(gè)按鈕的點(diǎn)擊事件,以此來(lái)響應(yīng)泡泡的點(diǎn)擊,但是如果有很多的標(biāo)注,需要最點(diǎn)擊的泡泡遍歷,否則則不能知道點(diǎn)擊的是哪個(gè)泡泡. 我根據(jù)從服務(wù)器獲取的數(shù)據(jù),服務(wù)器返回了有關(guān)標(biāo)注的ID,把這個(gè)ID設(shè)置為paopaoView上按鈕的tag值,然后根據(jù)按鈕tag值來(lái)判斷點(diǎn)擊的是哪個(gè)paopaoView.如下代碼:
#pragma mark paopao按鈕點(diǎn)擊
- (void)detailBtnClick:(UIButton *)button{
//self.annotations是標(biāo)注數(shù)組
for (int i = 0; i < self.annotations.count; i++) {
HXMenDList *menDList = self.annotations[i];
if (button.tag == [menDList.shopId integerValue]) {
HXDetailsController *detailsV = [[HXDetailsController alloc] init];
detailsV.menDList = menDList;
detailsV.coordinate = self.userLocation.location.coordinate;
[self.navigationController pushViewController:detailsV animated:YES];
}
}
}