iOS百度地圖簡單使用詳解
百度地圖 iOS SDK是一套基于iOS 5.0及以上版本設(shè)備的應(yīng)用程序接口,不僅提供展示地圖的基本接口,還提供POI檢索、路徑規(guī)劃、地圖標(biāo)注、離線地圖、定位、周邊雷達(dá)等豐富的LBS能力 。
今天主要介紹以下接口
- 基礎(chǔ)地圖
- POI檢索
- 定位
首先配置環(huán)境
1.自動配置.framework形式開發(fā)包(使用CocoaPods)<推薦>
2.手動配置.framework形式開發(fā)包
特別注意:
(API里有很多注意點(diǎn),大家可以具體去看.但是我說的后兩點(diǎn)少其中一個(gè)都會失敗,第一點(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起,為了對iOS8的定位能力做兼容,需要在info.plist里添加(以下二選一,兩個(gè)都添加默認(rèn)使用 NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允許在前臺使用時(shí)獲取GPS的描述
NSLocationAlwaysUsageDescription ,允許永久使用GPS的描述
3、在使用Xcode6進(jìn)行SDK開發(fā)過程中,需要在info.plist中添加:Bundle display name ,且其值不能為空(Xcode6新建的項(xiàng)目沒有此配置,若沒有會造成manager start fail
配置完成后
AppDelegate.m文件中添加對BMKMapManager的初始化,并填入申請的授權(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è)引擎對象 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.基礎(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)前可使用的級別為3-21級 self.mapView.zoomLevel = 21; //設(shè)定地圖View能否支持旋轉(zhuǎn) self.mapView.rotateEnabled = NO; //設(shè)定地圖View能否支持用戶移動地圖 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 /** *用戶位置更新后,會調(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)前可使用的級別為3-21級 // 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 /** *用戶位置更新后,會調(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è)周邊云檢索對象 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 搜索對象 *@param poiResult 搜索結(jié)果列表 *@param errorCode 錯(cuò)誤號,@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 搜索對象 *@param poiDetailResult 詳情搜索結(jié)果 *@param errorCode 錯(cuò)誤號,@see BMKSearchErrorCode */ - (void)onGetPoiDetailResult:(BMKPoiSearch *)searcher result:(BMKPoiDetailResult *)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode { NSLog(@"%@",poiDetailResult.name); } #pragma mark -------------BMKMapViewDelegate /** *根據(jù)anntation生成對應(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è)置動畫 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)大,還有很多檢索,都沒有寫.大家又興趣可以鉆研下,畢竟第三方的接口文檔相對比較明了.以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ios開發(fā)navigationController pushViewController 方式多次跳轉(zhuǎn)返回到最上層返回到
這篇文章主要介紹了ios開發(fā)navigationController pushViewController 方式多次跳轉(zhuǎn)返回到最上層返回到指定的某一層的實(shí)現(xiàn)方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09iOS的UI開發(fā)中UITabBarControlle的基本使用教程
這篇文章主要介紹了iOS的UI開發(fā)中UITabBarControlle的基本使用教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12