ios百度地圖開發(fā)之顯示標注

demo 地址

MyAnnotation.h文件

#import <Foundation/Foundation.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
@interface MyAnnotation : BMKPointAnnotation
/** 
 * 圖標 
 */
@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]; 
  // 如果緩存池中沒有, 創(chuàng)建一個新的 
  if (annoView == nil) { 
    annoView = [[MyAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier]; 
  } 
  return annoView;
}
- (void)setAnnotation:(MyAnnotation *)annotation{ 
  [super setAnnotation:annotation]; 
  //設(shè)置圖標 
  self.image = [UIImage imageNamed:@"icon_green"];
}
@end

之后在控制器文件中,首先要先從服務(wù)器獲取數(shù)據(jù),取到數(shù)據(jù)后,根據(jù)經(jīng)緯度坐標,也就是一個個的Annotation顯示到地圖上.

//可以使用,anno是標注
[_mapView addAnnotation:_mapView.annotations];
//或者使用,annos是標注數(shù)組
[_mapView addAnnotations:_mapView.annotations];

之后在調(diào)用BMKMapViewDelegate的方法:

#pragma mark -BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation{ 
    // 對用戶當(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會有如下方法:

-(void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)

我在開發(fā)過程中,調(diào)用上面的方法并沒有作用,所以我在自定義的paopaoView里添加了一個按鈕,蓋滿整個paopaoView,之后添加一個按鈕的點擊事件,以此來響應(yīng)泡泡的點擊,但是如果有很多的標注,需要最點擊的泡泡遍歷,否則則不能知道點擊的是哪個泡泡. 我根據(jù)從服務(wù)器獲取的數(shù)據(jù),服務(wù)器返回了有關(guān)標注的ID,把這個ID設(shè)置為paopaoView上按鈕的tag值,然后根據(jù)按鈕tag值來判斷點擊的是哪個paopaoView.如下代碼:

#pragma mark paopao按鈕點擊
- (void)detailBtnClick:(UIButton *)button{
 //self.annotations是標注數(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];
   }
 }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,983評論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評論 19 139
  • by千點墨 01 ▼ 身邊不少姑娘,年過三十了仍然單身,不是遇不到愛情,也不是不想結(jié)婚,而是她們有能力自己活得漂亮...
    千點墨閱讀 210評論 0 0
  • 初夏,天氣暖和了,人們身心也舒展了。 杏花、桃花、梨花、蘋果花全部免了,果子開始膨大,脫掉花瓣的嫩果頂部還托著干了...
    白楊的心雨閱讀 445評論 0 5

友情鏈接更多精彩內(nèi)容