百度地圖 iOS SDK是一套基于iOS 5.0及以上版本設(shè)備的應(yīng)用程序接口,不僅提供展示地圖的基本接口烫止,還提供POI檢索、路徑規(guī)劃戳稽、地圖標(biāo)注馆蠕、離線地圖、定位惊奇、周邊雷達(dá)等豐富的LBS能力 互躬。
今天主要介紹以下接口
1.基礎(chǔ)地圖
2.POI檢索
3.定位
首先配置環(huán)境
1.自動(dòng)配置.framework形式開發(fā)包(使用CocoaPods)<推薦>
2.手動(dòng)配置.framework形式開發(fā)包
特別注意:
(API里有很多注意點(diǎn),大家可以具體去看.但是我說的后兩點(diǎn)少其中一個(gè)都會(huì)失敗,第一點(diǎn)是有需求的話,必須加上)
1、如果在iOS9中使用了調(diào)起百度地圖客戶端功能颂郎,必須在"Info.plist"中進(jìn)行如下配置吼渡,否則不能調(diào)起百度地圖客戶端。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>baidumap</string>
</array>
2乓序、自iOS SDK v2.5.0起寺酪,為了對(duì)iOS8的定位能力做兼容,需要在info.plist里添加(以下二選一替劈,兩個(gè)都添加默認(rèn)使用 NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription 寄雀,允許在前臺(tái)使用時(shí)獲取GPS的描述
NSLocationAlwaysUsageDescription ,允許永久使用GPS的描述
3陨献、在使用Xcode6進(jìn)行SDK開發(fā)過程中盒犹,需要在info.plist中添加:Bundle display name ,且其值不能為空(Xcode6新建的項(xiàng)目沒有此配置眨业,若沒有會(huì)造成manager start fail
配置完成后
AppDelegate.m文件中添加對(duì)BMKMapManager的初始化急膀,并填入申請(qǐng)的授權(quán)Key
#import "AppDelegate.h"
#import <BaiduMapAPI_Base/BMKMapManager.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//創(chuàng)建并初始化一個(gè)引擎對(duì)象
BMKMapManager *manager = [[BMKMapManager alloc] init];
//啟動(dòng)地圖引擎
BOOL success = [manager start:@"zBWLNgRUrTp9CVb5Ez6gZpNebljmYylO" generalDelegate:nil];
if (!success) {
NSLog(@"失敗");
}
// Override point for customization after application launch.
return YES;
}
1.基礎(chǔ)地圖
#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;
//設(shè)置地圖的顯示樣式
self.mapView.mapType = BMKMapTypeSatellite;//衛(wèi)星地圖
//設(shè)定地圖是否打開路況圖層
self.mapView.trafficEnabled = YES;
//底圖poi標(biāo)注
self.mapView.showMapPoi = NO;
//在手機(jī)上當(dāng)前可使用的級(jí)別為3-21級(jí)
self.mapView.zoomLevel = 21;
//設(shè)定地圖View能否支持旋轉(zhuǎn)
self.mapView.rotateEnabled = NO;
//設(shè)定地圖View能否支持用戶移動(dòng)地圖
self.mapView.scrollEnabled = NO;
//添加到view上
[self.view addSubview:self.mapView];
//還有很多屬性,根據(jù)需求查看API
}
運(yùn)行效果入下;
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;//定位服務(wù)
@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];
//設(shè)置代理
self.service.delegate = self;
//開啟定位
[self.service startUserLocationService];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark -------BMKLocationServiceDelegate
/**
*用戶位置更新后,會(huì)調(diào)用此函數(shù)
*@param userLocation 新的用戶位置
*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
//展示定位
self.mapView.showsUserLocation = YES;
//更新位置數(shù)據(jù)
[self.mapView updateLocationData:userLocation];
//獲取用戶的坐標(biāo)
self.mapView.centerCoordinate = userLocation.location.coordinate;
self.mapView.zoomLevel =18;
}
運(yùn)行結(jié)果
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;//定位服務(wù)
@property (nonatomic,strong) BMKPoiSearch *poiSearch;//搜索服務(wù)
@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;
// //設(shè)置地圖的顯示樣式
// self.mapView.mapType = BMKMapTypeSatellite;//衛(wèi)星地圖
//
// //設(shè)置路況
// self.mapView.trafficEnabled = YES;
//
// //底圖poi標(biāo)注
// self.mapView.showMapPoi = NO;
//
// //在手機(jī)上當(dāng)前可使用的級(jí)別為3-21級(jí)
// self.mapView.zoomLevel = 21;
//
// //旋轉(zhuǎn)
// self.mapView.rotateEnabled = NO;
//
// //拖拽
// self.mapView.scrollEnabled = NO;
//
[self.view addSubview:self.mapView];
//初始化定位
self.service = [[BMKLocationService alloc] init];
//設(shè)置代理
self.service.delegate = self;
//開啟定位
[self.service startUserLocationService];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark -------BMKLocationServiceDelegate
/**
*用戶位置更新后龄捡,會(huì)調(diào)用此函數(shù)
*@param userLocation 新的用戶位置
*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
//展示定位
self.mapView.showsUserLocation = YES;
//更新位置數(shù)據(jù)
[self.mapView updateLocationData:userLocation];
//獲取用戶的坐標(biāo)
self.mapView.centerCoordinate = userLocation.location.coordinate;
self.mapView.zoomLevel =18;
//初始化搜索
self.poiSearch =[[BMKPoiSearch alloc] init];
self.poiSearch.delegate = self;
//初始化一個(gè)周邊云檢索對(duì)象
BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc] init];
//索引 默認(rèn)為0
option.pageIndex = 0;
//頁數(shù)默認(rèn)為10
option.pageCapacity = 50;
//搜索半徑
option.radius = 200;
//檢索的中心點(diǎn)卓嫂,經(jīng)緯度
option.location = userLocation.location.coordinate;
//搜索的關(guān)鍵字
option.keyword = @"小吃";
//根據(jù)中心點(diǎn)、半徑和檢索詞發(fā)起周邊檢索
BOOL flag = [self.poiSearch poiSearchNearBy:option];
if (flag) {
NSLog(@"搜索成功");
//關(guān)閉定位
[self.service stopUserLocationService];
}
else {
NSLog(@"搜索失敗");
}
}
#pragma mark -------BMKPoiSearchDelegate
/**
*返回POI搜索結(jié)果
*@param searcher 搜索對(duì)象
*@param poiResult 搜索結(jié)果列表
*@param errorCode 錯(cuò)誤號(hào)墅茉,@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];
//初始化一個(gè)點(diǎn)的注釋 //只有三個(gè)屬性
BMKPointAnnotation *annotoation = [[BMKPointAnnotation alloc] init];
//坐標(biāo)
annotoation.coordinate = info.pt;
//title
annotoation.title = info.name;
//子標(biāo)題
annotoation.subtitle = info.address;
//將標(biāo)注添加到地圖上
[self.mapView addAnnotation:annotoation];
}
}
}
/**
*返回POI詳情搜索結(jié)果
*@param searcher 搜索對(duì)象
*@param poiDetailResult 詳情搜索結(jié)果
*@param errorCode 錯(cuò)誤號(hào)命黔,@see BMKSearchErrorCode
*/
- (void)onGetPoiDetailResult:(BMKPoiSearch *)searcher result:(BMKPoiDetailResult *)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode {
NSLog(@"%@",poiDetailResult.name);
}
#pragma mark -------------BMKMapViewDelegate
/**
*根據(jù)anntation生成對(duì)應(yīng)的View
*@param mapView 地圖View
*@param annotation 指定的標(biāo)注
*@return 生成的標(biāo)注View
*/
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
//如果是注釋點(diǎn)
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
//根據(jù)注釋點(diǎn),創(chuàng)建并初始化注釋點(diǎn)視圖
BMKPinAnnotationView *newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"an"];
//設(shè)置大頭針的顏色
newAnnotation.pinColor = BMKPinAnnotationColorRed;
//設(shè)置動(dòng)畫
newAnnotation.animatesDrop = YES;
return newAnnotation;
}
return nil;
}
/**
*當(dāng)選中一個(gè)annotation views時(shí)呜呐,調(diào)用此接口
*@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結(jié)構(gòu)中獲取
option.poiUid = info.uid;
/**
*根據(jù)poi uid 發(fā)起poi詳情檢索
*異步函數(shù)悍募,返回結(jié)果在BMKPoiSearchDelegate的onGetPoiDetailResult通知
*@param option poi詳情檢索參數(shù)類(BMKPoiDetailSearchOption)
*@return 成功返回YES蘑辑,否則返回NO
*/
BOOL flag = [self.poiSearch poiDetailSearch:option];
if (flag) {
NSLog(@"檢索成功");
}
else {
NSLog(@"檢索失敗");
}
}
運(yùn)行結(jié)果
總結(jié)
百度地圖的功能很強(qiáng)大,還有很多檢索,都沒有寫.大家又興趣可以鉆研下,畢竟第三方的接口文檔相對(duì)比較明了.如有問題,可以留言