热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOS百度地图开发集成使用

项目需要集成百度地图,那么关于如何集成百度地图的事,就自己去百度开放平台查看文档吧,这是非常简单的事,在这里就不多说了。那么下面我就说说我在这个

项目需要集成百度地图,那么关于如何集成百度地图的事,就自己去百度开放平台查看文档吧,这是非常简单的事,在这里就不多说了。

那么下面我就说说我在这个demo里所做的事。

首先,项目需要具备定位及计算两地的距离

其次,项目需要根据两个地点来拿到所有路线,并且可根据不同的策略拿到对应的最佳路线。

最后,需要拿到打车相关信息


那么这里我就自己写了一个单例类,这是在内部处理所有的代理,外部可以非常方便地调用,如果有好的建议,请在评论中赐教,谢谢!

//
// HYBBaiduMapHelper.h
// BaiduMapDemo
//
// Created by 黄仪标 on 14/11/18.
// Copyright (c) 2014年 黄仪标. All rights reserved.
//#import
#import "BMapKit.h"typedef void (^HYBUserLocationCompletion)(BMKUserLocation *userLocation);
typedef void (^HYBRouteSearchCompletion)(BMKTransitRouteResult *result);/*!* @brief 百度地图相关API操作类** @author huangyibiao*/
@interface HYBBaiduMapHelper : NSObject+ (HYBBaiduMapHelper *)shared;///
/// 该方法在appdelegate的调用,在应用启动时,请求授权百度地图
- (BOOL)startWithAppKey:(NSString *)appKey;///
/// 下面的几个方法是定位使用
- (void)locateInView:(UIView *)mapSuerViewframe:(CGRect)framewithCompletion:(HYBUserLocationCompletion)completion;
- (void)viewWillAppear;
- (void)viewWillDisappear;
- (void)viewDidDeallocOrReceiveMemoryWarning;///
/// 下面的方法是计算两地的距离
/// 返回距离单位为米
- (CLLocationDistance)distanceWithStartPoint:(CLLocationCoordinate2D)startPointendPoint:(CLLocationCoordinate2D)endPoint;///
/// 下面的方法是路线规划获取操作
/// 注意:不能同时调用下面的这三个方法,必须是先调用完一个,返回结果后,再继续调用别的,否则会覆盖前一个操作的数据/// 公交检索方法
/// 前两个参数,分别表示起点和终点的位置名称
/// 第三个参数,表示在哪个城市里检索
- (void)transitRouteSearchFrom:(BMKPlanNode *)startNodeto:(BMKPlanNode *)endNodecity:(NSString *)citytransitPolicy:(BMKTransitPolicy)transitPolicycompletion:(HYBRouteSearchCompletion)completion;/// 驾乘检索方法
/// 前两个参数,分别表示起点和终点的位置名称
- (void)driveRouteSearchFrom:(BMKPlanNode *)startNodeto:(BMKPlanNode *)endNodedrivePolicy:(BMKDrivingPolicy)drivePolicycompletion:(HYBRouteSearchCompletion)completion;/// 步行检索方法
/// 前两个参数,分别表示起点和终点的位置名称
- (void)walkRouteSearchFrom:(BMKPlanNode *)startNodeto:(BMKPlanNode *)endNodecompletion:(HYBRouteSearchCompletion)completion;@end


//
// HYBBaiduMapHelper.m
// BaiduMapDemo
//
// Created by 黄仪标 on 14/11/18.
// Copyright (c) 2014年 黄仪标. All rights reserved.
//#import "HYBBaiduMapHelper.h"@interface HYBBaiduMapHelper () BMKGeneralDelegate,
BMKMapViewDelegate,
BMKRouteSearchDelegate> {BMKMapManager *_mapManager;HYBUserLocationCompletion _locationCompletion;HYBRouteSearchCompletion _routeSearchCompletion;BMKMapView *_mapView;BMKLocationService *_locationService;BMKRouteSearch *_routeSearch;
}@end@implementation HYBBaiduMapHelper+ (HYBBaiduMapHelper *)shared {static HYBBaiduMapHelper *baiduMapHelper = nil;static dispatch_once_t onceToken = 0;dispatch_once(&onceToken, ^{if (!baiduMapHelper) {baiduMapHelper = [[[self class] alloc] init];}});return baiduMapHelper;
}- (instancetype)init {if (self = [super init]) {_mapManager = [[BMKMapManager alloc] init];}return self;
}- (BOOL)startWithAppKey:(NSString *)appKey {if (![appKey isKindOfClass:[NSString class]] || appKey.length == 0 || appKey == nil) {return NO;}return [_mapManager start:appKey generalDelegate:self];
}- (void)locateInView:(UIView *)mapSuerView frame:(CGRect)frame withCompletion:(HYBUserLocationCompletion)completion {_locationCompletion = [completion copy];[_locationService stopUserLocationService];_locationService = nil;_locationService.delegate = nil;_locationService = [[BMKLocationService alloc] init];[_locationService startUserLocationService];if (_mapView) {[_mapView removeFromSuperview];_mapView = nil;}_mapView.delegate = nil;_mapView.showsUserLocation = NO;_mapView = [[BMKMapView alloc] initWithFrame:frame];[mapSuerView addSubview:_mapView];_mapView.delegate = self;// 先关闭显示的定位图层_mapView.showsUserLocation = NO;// 设置定位的状态_mapView.userTrackingMode = BMKUserTrackingModeNone;_mapView.showsUserLocation = YES;return;
}- (void)viewWillAppear {[_mapView viewWillAppear];_mapView.delegate = self;_locationService.delegate = self;_routeSearch.delegate = self;return;
}- (void)viewWillDisappear {[_mapView viewWillDisappear];_mapView.delegate = nil;_locationService.delegate = nil;_routeSearch.delegate = nil;return;
}- (void)viewDidDeallocOrReceiveMemoryWarning {[self viewWillDisappear];_mapView.showsUserLocation = NO;[_locationService stopUserLocationService];[_mapView removeFromSuperview];_mapView = nil;_locationService = nil;_routeSearch.delegate = nil;_routeSearch = nil;return;
}///
/// 计算两点的距离
- (CLLocationDistance)distanceWithStartPoint:(CLLocationCoordinate2D)startPoint endPoint:(CLLocationCoordinate2D)endPoint {BMKMapPoint point1 = BMKMapPointForCoordinate(startPoint);BMKMapPoint point2 = BMKMapPointForCoordinate(endPoint);CLLocationDistance distance = BMKMetersBetweenMapPoints(point1, point2);return distance;
}///
/// 下面的方法是路线规划获取操作
/// 公交检索方法
/// 前两个参数,分别表示起点和终点的位置名称
/// 第三个参数,表示在哪个城市里检索
- (void)transitRouteSearchFrom:(BMKPlanNode *)startNodeto:(BMKPlanNode *)endNodecity:(NSString *)citytransitPolicy:(BMKTransitPolicy)transitPolicycompletion:(HYBRouteSearchCompletion)completion {_routeSearchCompletion = [completion copy];if (_routeSearch == nil) {_routeSearch = [[BMKRouteSearch alloc] init];}_routeSearch.delegate = self;// 公交检索BMKTransitRoutePlanOption *transitRoutePlan = [[BMKTransitRoutePlanOption alloc] init];transitRoutePlan.city = city;transitRoutePlan.from = startNode;transitRoutePlan.to = endNode;transitRoutePlan.transitPolicy = transitPolicy;if ([_routeSearch transitSearch:transitRoutePlan]) {NSLog(@"bus检索发送成功");} else {NSLog(@"bus检索发送失败");}return;
}/// 驾乘检索方法
/// 前两个参数,分别表示起点和终点的位置名称
- (void)driveRouteSearchFrom:(BMKPlanNode *)startNodeto:(BMKPlanNode *)endNodedrivePolicy:(BMKDrivingPolicy)drivePolicycompletion:(HYBRouteSearchCompletion)completion {_routeSearchCompletion = [completion copy];if (_routeSearch == nil) {_routeSearch = [[BMKRouteSearch alloc] init];}_routeSearch.delegate = self;// 公交检索BMKDrivingRoutePlanOption *driveRoutePlan = [[BMKDrivingRoutePlanOption alloc] init];driveRoutePlan.from = startNode;driveRoutePlan.to = endNode;driveRoutePlan.drivingPolicy = drivePolicy;if ([_routeSearch drivingSearch:driveRoutePlan]) {NSLog(@"drive 检索发送成功");} else {NSLog(@"drive 检索发送失败");}return;
}/// 步行检索方法
/// 前两个参数,分别表示起点和终点的位置名称
- (void)walkRouteSearchFrom:(BMKPlanNode *)startNodeto:(BMKPlanNode *)endNodecompletion:(HYBRouteSearchCompletion)completion {_routeSearchCompletion = [completion copy];if (_routeSearch == nil) {_routeSearch = [[BMKRouteSearch alloc] init];}_routeSearch.delegate = self;// 公交检索BMKWalkingRoutePlanOption *walkRoutePlan = [[BMKWalkingRoutePlanOption alloc] init];walkRoutePlan.from = startNode;walkRoutePlan.to = endNode;if ([_routeSearch walkingSearch:walkRoutePlan]) {NSLog(@"walk 检索发送成功");} else {NSLog(@"walk 检索发送失败");}return;
}#pragma mark - BMKGeneralDelegate
- (void)onGetNetworkState:(int)iError {if (0 == iError) {NSLog(@"联网成功");} else {NSLog(@"onGetNetworkState %d",iError);}return;
}- (void)onGetPermissionState:(int)iError {if (0 == iError) {NSLog(@"百度地图授权成功");} else {NSLog(@"onGetPermissionState %d",iError);}return;
}#pragma mark - BMKLocationServiceDelegate
/***在将要启动定位时,会调用此函数*/
- (void)willStartLocatingUser {NSLog(@"location start");return;
}/***在停止定位后,会调用此函数*/
- (void)didStopLocatingUser {NSLog(@"user location stop");return;
}/***用户方向更新后,会调用此函数*@param userLocation 新的用户位置*/
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation {NSLog(@"user derection change");[_mapView updateLocationData:userLocation];return;
}/***用户位置更新后,会调用此函数*@param userLocation 新的用户位置*/
- (void)didUpdateUserLocation:(BMKUserLocation *)userLocation {NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);[_mapView updateLocationData:userLocation];if (_locationCompletion) {_locationCompletion(userLocation);}[_locationService stopUserLocationService];return;
}/***定位失败后,会调用此函数*@param error 错误号*/
- (void)didFailToLocateUserWithError:(NSError *)error {if (_locationCompletion) {_locationCompletion(nil);}[_locationService stopUserLocationService];return;
}#pragma mark - BMKRouteSearchDelegate
- (void)onGetTransitRouteResult:(BMKRouteSearch *)searcherresult:(BMKTransitRouteResult *)resulterrorCode:(BMKSearchErrorCode)error {if (error == BMK_SEARCH_NO_ERROR) { // 检索成功的处理for (BMKTransitRouteLine *line in result.routes) {NSLog(@"-----------------------------------------------------");NSLog(@" 时间:%2d %2d:%2d:%2d 长度: %d米",line.duration.dates,line.duration.hours,line.duration.minutes,line.duration.seconds,line.distance);for (BMKTransitStep *step in line.steps) {NSLog(@"%@ %@ %@ %@ %@",step.entrace.title,step.exit.title,step.instruction,(step.stepType == BMK_BUSLINE ? @"公交路段" : (step.stepType == BMK_SUBWAY ? @"地铁路段" : @"步行路段")),[NSString stringWithFormat:@"名称:%@ 所乘站数:%d 全程价格:%d 区间价格:%d",step.vehicleInfo.title,step.vehicleInfo.passStationNum,step.vehicleInfo.totalPrice,step.vehicleInfo.zonePrice]);}}// 打车信息NSLog(@"打车信息------------------------------------------");NSLog(@"路线打车描述信息:%@ 总路程: %d米 总耗时:约%f分钟 每千米单价:%f元 全程总价:约%d元",result.taxiInfo.desc,result.taxiInfo.distance,result.taxiInfo.duration / 60.0,result.taxiInfo.perKMPrice,result.taxiInfo.totalPrice);} else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR) { // 检索地址有岐义,可获取推荐地址// 获取建议检索起终点NSLog(@"无检索结果,返回了建议检索信息");NSLog(@"起点推荐信息:--------------------------------");for (BMKPoiInfo *info in result.suggestAddrResult.startPoiList) {NSLog(@"POI名称:%@ POI地址:%@ POI所在城市:%@", info.name, info.address, info.city);}NSLog(@"终点推荐信息:--------------------------------");for (BMKPoiInfo *info in result.suggestAddrResult.endPoiList) {NSLog(@"POI名称:%@ POI地址:%@ POI所在城市:%@", info.name, info.address, info.city);}} else {NSLog(@"无公交检索结果 ");}// 回调block根据实际需要返回,可修改返回结构if (_routeSearchCompletion) {_routeSearchCompletion(nil); // 这里只是返回空,这个需要根据实际需要返回}return;
}- (void)onGetDrivingRouteResult:(BMKRouteSearch *)searcherresult:(BMKDrivingRouteResult *)resulterrorCode:(BMKSearchErrorCode)error {if (error == BMK_SEARCH_NO_ERROR) { // 检索成功的处理for (BMKDrivingRouteLine *line in result.routes) {NSLog(@"-----------------------------------------------------");NSLog(@" 时间:%2d %2d:%2d:%2d 长度: %d米",line.duration.dates,line.duration.hours,line.duration.minutes,line.duration.seconds,line.distance);for (BMKDrivingStep *step in line.steps) {NSLog(@"入口:%@ 出口:%@ 路段总体指示信息:%@ 入口信息:%@ 出口信息:%@ 转弯数:%d",step.entrace.title,step.exit.title,step.instruction,step.entraceInstruction,step.exitInstruction,step.numTurns);}}} else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR) { // 检索地址有岐义,可获取推荐地址// 获取建议检索起终点NSLog(@"无检索结果,返回了建议检索信息");NSLog(@"起点推荐信息:--------------------------------");for (BMKPoiInfo *info in result.suggestAddrResult.startPoiList) {NSLog(@"POI名称:%@ POI地址:%@ POI所在城市:%@", info.name, info.address, info.city);}NSLog(@"终点推荐信息:--------------------------------");for (BMKPoiInfo *info in result.suggestAddrResult.endPoiList) {NSLog(@"POI名称:%@ POI地址:%@ POI所在城市:%@", info.name, info.address, info.city);}} else {NSLog(@"无公交检索结果 ");}// 回调block根据实际需要返回,可修改返回结构if (_routeSearchCompletion) {_routeSearchCompletion(nil); // 这里只是返回空,这个需要根据实际需要返回}return;
}- (void)onGetWalkingRouteResult:(BMKRouteSearch *)searcherresult:(BMKWalkingRouteResult *)resulterrorCode:(BMKSearchErrorCode)error {if (error == BMK_SEARCH_NO_ERROR) { // 检索成功的处理for (BMKDrivingRouteLine *line in result.routes) {NSLog(@"步行检索结果 :-----------------------------------------------------");NSLog(@" 时间:%2d %2d:%2d:%2d 长度: %d米",line.duration.dates,line.duration.hours,line.duration.minutes,line.duration.seconds,line.distance);for (BMKWalkingStep *step in line.steps) {NSLog(@"入口:%@ 出口:%@ 路段总体指示信息:%@ 入口信息:%@ 出口信息:%@",step.entrace.title,step.exit.title,step.instruction,step.entraceInstruction,step.exitInstruction);}}} else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR) { // 检索地址有岐义,可获取推荐地址// 获取建议检索起终点NSLog(@"无检索结果,返回了建议检索信息");NSLog(@"起点推荐信息:--------------------------------");for (BMKPoiInfo *info in result.suggestAddrResult.startPoiList) {NSLog(@"POI名称:%@ POI地址:%@ POI所在城市:%@", info.name, info.address, info.city);}NSLog(@"终点推荐信息:--------------------------------");for (BMKPoiInfo *info in result.suggestAddrResult.endPoiList) {NSLog(@"POI名称:%@ POI地址:%@ POI所在城市:%@", info.name, info.address, info.city);}} else {NSLog(@"无公交检索结果 ");}// 回调block根据实际需要返回,可修改返回结构if (_routeSearchCompletion) {_routeSearchCompletion(nil); // 这里只是返回空,这个需要根据实际需要返回}return;
}@end





下面就是测试一个我们的数据是否真的拿到了:

//
// RootViewController.m
// BaiduMapDemo
//
// Created by 黄仪标 on 14/11/18.
// Copyright (c) 2014年 黄仪标. All rights reserved.
//#import "RootViewController.h"
#import "HYBBaiduMapHelper.h"
#import "BMapKit.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad {[super viewDidLoad];// 功能1、定位[[HYBBaiduMapHelper shared] locateInView:self.view frame:self.view.bounds withCompletion:^(BMKUserLocation *userLocation) {NSLog(@"%f %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);}];// 功能2:”计算距离CLLocationDistance distance = [[HYBBaiduMapHelper shared] distanceWithStartPoint:CLLocationCoordinate2DMake(39.915,116.404)endPoint:CLLocationCoordinate2DMake(38.915,115.404)];NSLog(@"distance = %fm", distance);// 功能3:公交检索BMKPlanNode *startNode = [[BMKPlanNode alloc] init];startNode.name = @"梆子井";startNode.cityName = @"北京";BMKPlanNode *endNode = [[BMKPlanNode alloc] init];endNode.name = @"金长安大厦";endNode.cityName = @"北京";// 功能3:公交检索[[HYBBaiduMapHelper shared] transitRouteSearchFrom:startNode to:endNode city:@"北京" transitPolicy:BMK_TRANSIT_TRANSFER_FIRST completion:^(BMKTransitRouteResult *result) {// 功能4:驾乘检索[[HYBBaiduMapHelper shared] driveRouteSearchFrom:startNode to:endNode drivePolicy:BMK_DRIVING_TIME_FIRST completion:^(BMKTransitRouteResult *result) {// 功能5:步行检索[[HYBBaiduMapHelper shared] walkRouteSearchFrom:startNode to:endNode completion:^(BMKTransitRouteResult *result) {;}];}];}];return;
}- (void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];[[HYBBaiduMapHelper shared] viewWillAppear];return;
}- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:animated];[[HYBBaiduMapHelper shared] viewWillDisappear];return;
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];[[HYBBaiduMapHelper shared] viewDidDeallocOrReceiveMemoryWarning];return;
}- (void)dealloc {[[HYBBaiduMapHelper shared] viewDidDeallocOrReceiveMemoryWarning];return;
}@end


想要深入研究的同学,可以去官网看官方提供的Demo,

如果想在我的demo之上进一步追加或者修改功能,可以下载本demo。


推荐阅读
  • IOS开发之百度地图API环境搭建 ... [详细]
  • IOS笔记汇总为了方便开发者开发出强大的功能,苹果提供了各种各样的框架IOS属性IOS基础属性导入依赖propertyNSStringNSDictionaryNSAr ... [详细]
  • ML学习笔记20210824分类算法模型选择与调优
    3.模型选择和调优3.1交叉验证定义目的为了让模型得精度更加可信3.2超参数搜索GridSearch对K值进行选择。k[1,2,3,4,5,6]循环遍历搜索。API参数1& ... [详细]
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了iOS核心笔记—CoreLocation框架-基础相关的知识,希望对你有一定的参考价值。1、 ... [详细]
  • iPhone开发之地图定位(CoreLocation和Mapkit框架)简易编程 ... [详细]
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • 利用决策树预测NBA比赛胜负的Python数据挖掘实践
    本文通过使用2013-14赛季NBA赛程与结果数据集以及2013年NBA排名数据,结合《Python数据挖掘入门与实践》一书中的方法,展示如何应用决策树算法进行比赛胜负预测。我们将详细讲解数据预处理、特征工程及模型评估等关键步骤。 ... [详细]
  • 微信小程序中实现位置获取的全面指南
    本文详细介绍了如何在微信小程序中实现地理位置的获取,包括通过微信官方API和腾讯地图API两种方式。文中不仅涵盖了必要的准备工作,如申请开发者密钥、下载并配置SDK等,还提供了处理用户授权及位置信息获取的具体代码示例。 ... [详细]
  • IOS9之当前位置定位
    2019独角兽企业重金招聘Python工程师标准#import*.h文件中导入以下两个框架*#import ... [详细]
  • 添加#import头文件倒入mapkit.framework库mapkit.framework是属于ui,可以在故事版上添加mkmap ... [详细]
  • 高德IOS真机调试LMAMapKit出错问题的解决方法(directory not found for option)
    1最近项目到了收尾阶段,所以开始了在真机部署调试,之前的模拟器环境下测试项目,一切都没问题,今天真机调试过程中, ... [详细]
  • MRMapViewController.mCoreLocation&MapKitCreatedbyMr.Roboton201786. ... [详细]
  • iOS 地图导航路线规划详解
    虽然是转载的,还是说几句吧。网上百度地图导航路线规划倒是挺多的,苹果自带的高德导航确实挺少,研究了好久发现就这个讲的稍微全一点,把需要用到的类什么的都讲清楚了。不过高德有个方法可以跳转 ... [详细]
  • GameKit为游戏提供网络功能:点对点互联和游戏中的语音交流AddressBook提供访问用户联系人信息的功能AddressBookUI提供一个用户界面࿰ ... [详细]
  • iPhoneSDK提供了三个类来管理位置信息:CLLocationCLLocationManager和CLLHeading(不常用)。除了使用GPS来获取当前的位置信息外,iPho ... [详细]
author-avatar
ccsv0601604
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有