百度地圖 iOS SDK是一套基於iOS 5.0及以上版本裝置的應用程式介面,不僅提供展示地圖的基本介面,還提供POI檢索、路徑規劃、地表徵圖注、離線地圖、定位、周邊雷達等豐富的LBS能力 。
今天主要介紹以下介面
首先配置環境
1.自動設定.framework形式開發包(使用CocoaPods)<推薦>
2.手動設定.framework形式開發包
特別注意:
(API裡有很多注意點,大家可以具體去看.但是我說的後兩點少其中一個都會失敗,第一點是有需求的話,必須加上)
1、如果在iOS9中使用了調起百度地圖用戶端功能,必須在"Info.plist"中進行如下配置,否則不能調起百度地圖用戶端。
<key>LSApplicationQueriesSchemes</key> <array> <string>baidumap</string> </array>
2、自iOS SDK v2.5.0起,為了對iOS8的定位能力做相容,需要在info.plist裡添加(以下二選一,兩個都添加預設使用 NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允許在前台使用時擷取GPS的描述
NSLocationAlwaysUsageDescription ,允許永久使用GPS的描述
3、在使用Xcode6進行SDK開發過程中,需要在info.plist中添加:Bundle display name ,且其值不可為空(Xcode6建立的項目沒有此配置,若沒有會造成manager start fail
配置完成後
AppDelegate.m檔案中添加對BMKMapManager的初始化,並填入申請的授權Key
#import "AppDelegate.h"#import <BaiduMapAPI_Base/BMKMapManager.h>@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {//建立並初始化一個引擎對象 BMKMapManager *manager = [[BMKMapManager alloc] init];//啟動地圖引擎 BOOL success = [manager start:@"zBWLNgRUrTp9CVb5Ez6gZpNebljmYylO" generalDelegate:nil]; if (!success) { NSLog(@"失敗"); } // Override point for customization after application launch. return YES;}
1.基礎地圖
#import "ViewController.h"#import <BaiduMapAPI_Map/BMKMapView.h>@interface ViewController ()<BMKMapViewDelegate>@property (nonatomic,strong) BMKMapView *mapView;//地圖視圖@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //初始化地圖 self.mapView = [[BMKMapView alloc] initWithFrame:self.view.frame]; self.mapView.delegate =self; //設定地圖的顯示樣式 self.mapView.mapType = BMKMapTypeSatellite;//衛星地圖 //設定地圖是否開啟路況圖層 self.mapView.trafficEnabled = YES; //底圖poi標註 self.mapView.showMapPoi = NO; //在手機上當前可使用的層級為3-21級 self.mapView.zoomLevel = 21; //設定地圖View能否支援旋轉 self.mapView.rotateEnabled = NO; //設定地圖View能否支援使用者移動地圖 self.mapView.scrollEnabled = NO; //添加到view上 [self.view addSubview:self.mapView]; //還有很多屬性,根據需求查看API}
運行效果入下;
2.定位
#import "ViewController.h"#import <BaiduMapAPI_Map/BMKMapView.h>#import <BaiduMapAPI_Location/BMKLocationService.h>@interface ViewController ()<BMKLocationServiceDelegate,BMKMapViewDelegate>@property (nonatomic,strong) BMKMapView *mapView;//地圖視圖@property (nonatomic,strong) BMKLocationService *service;//定位服務@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //初始化地圖 self.mapView = [[BMKMapView alloc] initWithFrame:self.view.frame]; self.mapView.delegate =self; //添加到view上 [self.view addSubview:self.mapView]; //初始化定位 self.service = [[BMKLocationService alloc] init]; //設定代理 self.service.delegate = self; //開啟定位 [self.service startUserLocationService]; // Do any additional setup after loading the view, typically from a nib.}#pragma mark -------BMKLocationServiceDelegate /** *使用者位置更新後,會調用此函數 *@param userLocation 新的使用者位置 */- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation { //展示定位 self.mapView.showsUserLocation = YES; //更新位置資料 [self.mapView updateLocationData:userLocation]; //擷取使用者的座標 self.mapView.centerCoordinate = userLocation.location.coordinate; self.mapView.zoomLevel =18;}
運行結果
POI檢索
#import "ViewController.h"#import <BaiduMapAPI_Map/BMKMapView.h>#import <BaiduMapAPI_Location/BMKLocationService.h>#import <BaiduMapAPI_Search/BMKPoiSearch.h>#import <BaiduMapAPI_Map/BMKAnnotation.h>#import <BaiduMapAPI_Map/BMKPointAnnotation.h>#import <BaiduMapAPI_Map/BMKPinAnnotationView.h>#define kWidth [UIScreen mainScreen].bounds.size.width@interface ViewController ()<BMKLocationServiceDelegate,BMKPoiSearchDelegate,BMKMapViewDelegate>@property (nonatomic,strong) BMKMapView *mapView;//地圖視圖@property (nonatomic,strong) BMKLocationService *service;//定位服務@property (nonatomic,strong) BMKPoiSearch *poiSearch;//搜尋服務@property (nonatomic,strong) NSMutableArray *dataArray;@end@implementation ViewController- (NSMutableArray *)dataArray { if (!_dataArray) { _dataArray = [NSMutableArray array]; } return _dataArray;}- (void)viewDidLoad { [super viewDidLoad]; //初始化地圖 self.mapView = [[BMKMapView alloc] initWithFrame:self.view.frame]; self.mapView.delegate =self;// //設定地圖的顯示樣式// self.mapView.mapType = BMKMapTypeSatellite;//衛星地圖// // //設定路況// self.mapView.trafficEnabled = YES;// // //底圖poi標註// self.mapView.showMapPoi = NO;// // //在手機上當前可使用的層級為3-21級// self.mapView.zoomLevel = 21;// // //旋轉// self.mapView.rotateEnabled = NO;// // //拖拽// self.mapView.scrollEnabled = NO;// [self.view addSubview:self.mapView]; //初始化定位 self.service = [[BMKLocationService alloc] init]; //設定代理 self.service.delegate = self; //開啟定位 [self.service startUserLocationService]; // Do any additional setup after loading the view, typically from a nib.}#pragma mark -------BMKLocationServiceDelegate /** *使用者位置更新後,會調用此函數 *@param userLocation 新的使用者位置 */- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation { //展示定位 self.mapView.showsUserLocation = YES; //更新位置資料 [self.mapView updateLocationData:userLocation]; //擷取使用者的座標 self.mapView.centerCoordinate = userLocation.location.coordinate; self.mapView.zoomLevel =18; //初始化搜尋 self.poiSearch =[[BMKPoiSearch alloc] init]; self.poiSearch.delegate = self; //初始化一個周邊雲檢索對象 BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc] init]; //索引 預設為0 option.pageIndex = 0; //頁數預設為10 option.pageCapacity = 50; //搜尋半徑 option.radius = 200; //檢索的中心點,經緯度 option.location = userLocation.location.coordinate; //搜尋的關鍵字 option.keyword = @"小吃"; //根據中心點、半徑和檢索詞發起周邊檢索 BOOL flag = [self.poiSearch poiSearchNearBy:option]; if (flag) { NSLog(@"搜尋成功"); //關閉定位 [self.service stopUserLocationService]; } else { NSLog(@"搜尋失敗"); }}#pragma mark -------BMKPoiSearchDelegate/** *返回POI搜尋結果 *@param searcher 搜尋對象 *@param poiResult 搜尋結果列表 *@param errorCode 錯誤號碼,@see BMKSearchErrorCode */- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode { //若搜尋成功 if (errorCode ==BMK_SEARCH_NO_ERROR) { //POI資訊類 //poi列表 for (BMKPoiInfo *info in poiResult.poiInfoList) { [self.dataArray addObject:info]; //初始化一個點的注釋 //只有三個屬性 BMKPointAnnotation *annotoation = [[BMKPointAnnotation alloc] init]; //座標 annotoation.coordinate = info.pt; //title annotoation.title = info.name; //子標題 annotoation.subtitle = info.address; //將標註添加到地圖上 [self.mapView addAnnotation:annotoation]; } }}/** *返回POI詳情搜尋結果 *@param searcher 搜尋對象 *@param poiDetailResult 詳情搜尋結果 *@param errorCode 錯誤號碼,@see BMKSearchErrorCode */- (void)onGetPoiDetailResult:(BMKPoiSearch *)searcher result:(BMKPoiDetailResult *)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode { NSLog(@"%@",poiDetailResult.name);}#pragma mark -------------BMKMapViewDelegate/** *根據anntation產生對應的View *@param mapView 地圖View *@param annotation 指定的標註 *@return 產生的標註View */- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation { //如果是注釋點 if ([annotation isKindOfClass:[BMKPointAnnotation class]]) { //根據注釋點,建立並初始化注釋點視圖 BMKPinAnnotationView *newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"an"]; //設定圖釘的顏色 newAnnotation.pinColor = BMKPinAnnotationColorRed; //設定動畫 newAnnotation.animatesDrop = YES; return newAnnotation; } return nil;}/** *當選中一個annotation views時,調用此介面 *@param mapView 地圖View *@param views 選中的annotation views */- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view { //poi詳情檢索資訊類 BMKPoiDetailSearchOption *option = [[BMKPoiDetailSearchOption alloc] init]; BMKPoiInfo *info = self.dataArray.firstObject; //poi的uid,從poi檢索返回的BMKPoiResult結構中擷取 option.poiUid = info.uid; /** *根據poi uid 發起poi詳情檢索 *非同步函數,返回結果在BMKPoiSearchDelegate的onGetPoiDetailResult通知 *@param option poi詳情檢索參數類(BMKPoiDetailSearchOption) *@return 成功返回YES,否則返回NO */ BOOL flag = [self.poiSearch poiDetailSearch:option]; if (flag) { NSLog(@"檢索成功"); } else { NSLog(@"檢索失敗"); } }
運行結果
總結
百度地圖的功能很強大,還有很多檢索,都沒有寫.大家又興趣可以鑽研下,畢竟第三方的介面文檔相對比較明了.以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。