一、集成百度地圖(傻瓜教程,以網站說明文檔為准,此處羅列幾項主要步驟)
1、登錄 http://lbsyun.baidu.com 百度地圖開發者平台,獲取SDK和集成文檔。
2、百度地圖可以提供的一些服務
(1)地圖:提供地圖展示和地圖操作功能;
(2)POI檢索:支持周邊檢索、區域檢索和城市內興趣點檢索;
(3)地理編碼:提供經緯度和地址信息相互轉化的功能接口;
(4)線路規划:支持公交、駕車、步行、騎行,四種方式的線路規划;
(5)覆蓋物圖層:支持在地圖上添加覆蓋物(標注、幾何圖形、熱力圖、地形圖圖層等),展示更豐富的LBS信息;
(6)定位:獲取當前位置信息,並在地圖上展示(支持普通、跟隨、羅盤三種模式);
(7)離線地圖:使用離線地圖可節省用戶流量,提供更好的地圖展示效果;
(8)調啟百度地圖:利用SDK接口,直接在本地打開百度地圖客戶端或WebApp,實現地圖功能;
(9)周邊雷達:利用周邊雷達功能,開發者可在App內低成本、快速實現查找周邊使用相同App的用戶位置的功能;
(10)LBS雲檢索:支持查詢存儲在LBS雲內的自有數據;
(11)瓦片圖層: 支持在地圖上添加自有瓦片數據。
(12)特色功能:提供短串分享、Place詳情檢索、熱力圖等特色功能,幫助開發者搭建功能更加強大的應用;
2、創建應用,獲得AppKey (密鑰)
(1)這里需要以開發者身份獲得key
(2)注意和Bundle Identifier的綁定
3、配置集成前的注意事項
(1)嚴格參考文檔!
(2)幾個注意點
<1> iOS9.0之后,要在Info.plist配置ATS
<2> 在Info.plist配置 百度地圖應用的白名單(支持在自己的應用中打開百度地圖)
<3> 管理地圖的生命周期(手動將代理置空)
<4>在Info.plist中添加獲取定位授權的提醒
NSLocationWhenInUseUsageDescription ,允許在前台使用時獲取GPS的描述
NSLocationAlwaysUsageDescription ,允許永久使用GPS的描述
4、配置開發環境
(1)添加百度的靜態庫(.framework)
(2)導入一大堆的系統庫
(3)添加地圖包(mapapi.bundle資源文件)
(4)引入所有的頭文件
5、初始化地圖
(1)初始化BMKMapManager
(2)創建BMKMapView
6、基礎地圖
(1)參閱文檔!http://lbsyun.baidu.com/index.php?title=iossdk/guide/basicmap
(2)一些功能的了解
<1>地圖類型:普通矢量地圖、衛星圖和空白地圖(節省流量)
<2>打開實時路況圖層
<3>熱力地圖
<4>手勢、地圖控制、底標、折線(幾何圖形的遮蓋)
<5>設置地圖顯示范圍
<6> 等等等。。。
二、百度地圖POI檢索
#import "ViewController.h"
@interface ViewController ()<BMKMapViewDelegate,BMKPoiSearchDelegate>
@property(nonatomic,weak)BMKMapView * mapView;
@property(nonatomic,weak)UITextField * tf;
@property(nonatomic,strong)BMKPoiSearch * poisearch;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//設置地圖
BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.mapView = mapView;
[self.view addSubview:mapView];
//設置開始查找按鈕和輸入文本框
UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 100, 30)];
[btn setTitle:@"開始查找" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:btn];
UITextField * tf = [[UITextField alloc]initWithFrame:CGRectMake(150, 30, 100, 30)];
self.tf= tf;
tf.backgroundColor = [UIColor orangeColor];
[self.view addSubview:tf];
//實例POI檢索化檢索對象,並設置代理
self.poisearch = [[BMKPoiSearch alloc]init];
self.poisearch.delegate = self;
}
-(void)btnClick :(UIButton *)btn
{
//檢索參數
BMKCitySearchOption *citySearchOption = [[BMKCitySearchOption alloc]init];
citySearchOption.pageIndex = 0; //當前頁碼
citySearchOption.pageCapacity = 20; //每頁個數
citySearchOption.city= @"北京"; //城市
citySearchOption.keyword = self.tf.text; //關鍵字 酒店
//根據檢索參數,進行檢索,並返回檢索結果的標識
BOOL flag = [_poisearch poiSearchInCity:citySearchOption];
if(flag)
{
NSLog(@"城市內檢索發送成功");
}
else
{
NSLog(@"城市內檢索發送失敗");
}
}
#pragma mark implement BMKSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{
// 清除屏幕中所有的annotation
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];
//正確
if (error == BMK_SEARCH_NO_ERROR) {
NSMutableArray *annotations = [NSMutableArray array];
//遍歷返回的查詢結果
for (int i = 0; i < result.poiInfoList.count; i++) {
BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
item.coordinate = poi.pt;
item.title = poi.name;
[annotations addObject:item];
}
//給地圖添加大頭針模型
[_mapView addAnnotations:annotations];
[_mapView showAnnotations:annotations animated:YES];
} else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
NSLog(@"起始點有歧義");
} else {
// 各種情況的判斷。。。
}
}
#pragma mark implement BMKMapViewDelegate
/**
*根據anntation生成對應的View
*@param mapView 地圖View
*@param annotation 指定的標注
*@return 生成的標注View
*/
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
// 生成重用標示identifier
NSString *AnnotationViewID = @"xidanMark";
// 檢查是否有重用的緩存
BMKAnnotationView* annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
// 緩存沒有,自己構造一個,一般首次添加annotation代碼會運行到此處
if (annotationView == nil) {
annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed;
// 設置重天上掉下的效果(annotation)
((BMKPinAnnotationView*)annotationView).animatesDrop = YES;
}
// 設置位置
annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));
annotationView.annotation = annotation;
// 單擊彈出泡泡,彈出泡泡前提annotation必須實現title屬性
annotationView.canShowCallout = YES;
// 設置是否可以拖拽
annotationView.draggable = NO;
return annotationView;
}
-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此處記得不用的時候需要置nil,否則影響內存的釋放
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用時,置nil
}
@end
