ios百度地图接入流程

来源:互联网 发布:在线网络北京时间 编辑:程序博客网 时间:2024/05/01 22:13

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

今天主要介绍以下接口

1.基础地图
2.POI检索
3.定位

首先配置环境

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}

运行效果入下;


基本.jpg

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;}

运行结果


定位.jpg

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(@"检索失败");    }}

运行结果


搜索.jpg

总结

百度地图的功能很强大,还有很多检索,都没有写.大家又兴趣可以钻研下,毕竟第三方的接口文档相对比较明了.如有问题,可以留言



文/朱凯奇(简书作者)
原文链接:http://www.jianshu.com/p/39b56aace51c
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
0 0
原创粉丝点击