Welcome

首页 / 移动开发 / IOS / iOS百度地图简单使用详解

百度地图 iOS SDK是一套基于iOS 5.0及以上版本设备的应用程序接口,不仅提供展示地图的基本接口,还提供POI检索、路径规划、地图标注、离线地图、定位、周边雷达等丰富的LBS能力 。

今天主要介绍以下接口
  • 基础地图
  • POI检索
  • 定位
首先配置环境

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];//索引 默认为0option.pageIndex = 0;//页数默认为10option.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;//titleannotoation.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(@"检索失败");} }
运行结果

总结

百度地图的功能很强大,还有很多检索,都没有写.大家又兴趣可以钻研下,毕竟第三方的接口文档相对比较明了.以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。