iOS百度地图简单使用详解
百度地图 iOS SDK是一套基于iOS 5.0及以上版本设备的应用程序接口,不仅提供展示地图的基本接口,还提供POI检索、路径规划、地图标注、离线地图、定位、周边雷达等丰富的LBS能力 。
今天主要介绍以下接口
- 基础地图
- POI检索
- 定位
首先配置环境
1.自动配置.framework形式开发包(使用CocoaPods)<推荐>
2.手动配置.framework形式开发包
特别注意:
(API里有很多注意点,大家可以具体去看.但是我说的后两点少其中一个都会失败,第一点是有需求的话,必须加上)
1、如果在iOS9中使用了调起百度地图客户端功能,必须在"Info.plist"中进行如下配置,否则不能调起百度地图客户端。
| 1 2 3 4 | <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
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #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];//启动地图引擎  BOOLsuccess = [manager start:@"zBWLNgRUrTp9CVb5Ez6gZpNebljmYylO"generalDelegate:nil];  if(!success) {    NSLog(@"失败");  }  // Override point for customization after application launch.  returnYES;} | 
1.基础地图
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #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.定位
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #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检索
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | #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 = @"小吃";    //根据中心点、半径和检索词发起周边检索  BOOLflag = [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;    returnnewAnnotation;  }  returnnil;}/** *当选中一个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   */  BOOLflag = [self.poiSearch poiDetailSearch:option];  if(flag) {    NSLog(@"检索成功");  }  else{    NSLog(@"检索失败");  } } | 
运行结果
总结
百度地图的功能很强大,还有很多检索,都没有写.大家又兴趣可以钻研下,毕竟第三方的接口文档相对比较明了.以上就是本文的全部内容,希望对大家的学习有所帮助
 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号