ios开发中百度地图的基本使用二
来源:互联网 发布:淘宝买家延长收货 编辑:程序博客网 时间:2024/06/10 14:47
上篇介绍到了附近的POI搜索,在这篇我要给大家介绍一下路径的POI搜索。路径搜索分三种:步行、公交、自驾。写代码之前需要遵循BMKRouteSearchDelegate代理,注意代码中的起始点位置由用户自己手动输入,我的demo页面设计如下图
1.步行路线的搜索
- (IBAction)footBtnTouched:(id)sender {
_aView.hidden =YES;
search = [[BMKRouteSearchalloc]init];
search.delegate =self;
//发起检索
BMKPlanNode *start = [[BMKPlanNodealloc]init];
start.name =_serchTF.text;//搜索路径起点
start.cityName =@"北京市";
BMKPlanNode *end = [[BMKPlanNodealloc]init];
end.name =_search2TF.text;//搜索路径终点
end.cityName =@"北京市";
BMKWalkingRoutePlanOption *walkRouteSearchOption = [[BMKWalkingRoutePlanOptionalloc]init];
walkRouteSearchOption.from = start;
walkRouteSearchOption.to = end;
BOOL flag1 = [searchwalkingSearch:walkRouteSearchOption];
if(flag1)
{
NSLog(@"foot检索发送成功");
}
else
{
NSLog(@"foot检索发送失败");
}
}
#pragma mark -- 路线poi检索 --
-(void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error
{
NSArray* array = [NSArrayarrayWithArray:MapView.annotations];
[MapViewremoveAnnotations:array];
array = [NSArrayarrayWithArray:MapView.overlays];
[MapViewremoveOverlays:array];
if (error ==BMK_SEARCH_NO_ERROR) {
BMKWalkingRouteLine *plan = (BMKWalkingRouteLine*)[result.routesobjectAtIndex:0];//表示一条步行路线
int size = [plan.stepscount];
int planPointCounts = 0;
for (int i =0; i < size; i++) {
BMKWalkingStep *transitStep = [plan.stepsobjectAtIndex:i];//表示步行中的一个路段
if(i==0){
BMKPointAnnotation *item = [[BMKPointAnnotationalloc] init];
item.coordinate = plan.starting.location;
item.title =@"起点";
[MapViewaddAnnotation:item]; // 添加起点标注
}elseif(i==size-1){
BMKPointAnnotation *item = [[BMKPointAnnotationalloc] init];
item.coordinate = plan.terminal.location;
item.title =@"终点";
[MapViewaddAnnotation:item]; // 添加起点标注
}
//轨迹点
planPointCounts += transitStep.pointsCount;
}
BMKMapPoint *temppoints = new BMKMapPoint[planPointCounts];//地理坐标点
int i = 0;
for (int j =0; j < size; j++) {
BMKWalkingStep *transitStep = [plan.stepsobjectAtIndex:j];
for(int k=0;k<transitStep.pointsCount;k++) {
temppoints[i].x = transitStep.points[k].x;
temppoints[i].y = transitStep.points[k].y;
i++;
}
}
[MapView setCenterCoordinate:plan.starting.location];
// 通过points构建BMKPolyline
BMKPolyline *polyLine = [BMKPolylinepolylineWithPoints:temppoints count:planPointCounts];
[MapViewaddOverlay:polyLine]; // 添加路线overlay
}
elseif (error ==BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
//当路线起终点有歧义时通,获取建议检索起终点
}
else {
NSLog(@"抱歉,未找到结果");
}
search.delegate =nil;
}
#pragma mark -- 绘制路线折线图,此处实现的是BMKMapViewDelegate--
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKPolyline class]]){
BMKPolylineView* polylineView = [[BMKPolylineViewalloc] initWithOverlay:overlay];
polylineView.strokeColor = [[UIColorredColor] colorWithAlphaComponent:1];
polylineView.lineWidth =5.0;
return polylineView;
}
return nil;
}
此时的运行结果如下图所示- (IBAction)busBtnTouched:(id)sender {
search = [[BMKRouteSearchalloc]init];
search.delegate =self;
//发起检索
BMKPlanNode *start = [[BMKPlanNodealloc]init];
start.name =_serchTF.text;
BMKPlanNode *end = [[BMKPlanNodealloc]init];
end.name =_search2TF.text;
BMKTransitRoutePlanOption *transitRouteSearchOption = [[BMKTransitRoutePlanOptionalloc]init];
transitRouteSearchOption.city =@"北京市";//在那个城市搜索
transitRouteSearchOption.from = start;
transitRouteSearchOption.to = end;
BOOL flag1 = [searchtransitSearch:transitRouteSearchOption];
if(flag1)
{
NSLog(@"bus检索发送成功");
}
else
{
NSLog(@"bus检索发送失败");
}
}
#pragma mark -- 公交路线代理实现 --
- (void)onGetTransitRouteResult:(BMKRouteSearch *)searcher result:(BMKTransitRouteResult *)result errorCode:(BMKSearchErrorCode)error
{
[routArrremoveAllObjects];
[allRoutArrremoveAllObjects];
NSArray* array = [NSArrayarrayWithArray:MapView.annotations];
[MapViewremoveAnnotations:array];
array = [NSArrayarrayWithArray:MapView.overlays];
[MapViewremoveOverlays:array];
if (error ==BMK_SEARCH_NO_ERROR) {
for (int n =0; n< result.routes.count; n++) {
BMKTransitRouteLine *plan = (BMKTransitRouteLine*)[result.routesobjectAtIndex:n];
int size = [plan.stepscount];
for (int i =0; i < size; i++) {
BMKTransitStep *transitStep = [plan.stepsobjectAtIndex:i];//公交路线的换乘路段
transitStep.instruction//换乘路段说明,下面打印出来的结果就是每段的换乘说明,这个结果你想怎么出炉就怎么处理
}
}
}
}
下边是每个路段的换乘说明,大家可以按照自己的想法处理这个结果,至于每个路段经过的坐标点,大家可以参照步行的那个demo,基本都是一样的
- ios开发中百度地图的基本使用二
- ios开发中百度地图的基本使用一
- IOS 百度地图开发中大头针(标注)以及折线的基本使用
- iOS百度地图的基本使用
- 关于iOS百度地图的基本使用
- iOS百度地图的基本使用
- iOS百度地图的基本使用
- iOS开发 -- 百度地图api的使用
- ios 开发百度地图的使用
- IOS 百度地图开发 大头针标数字 大头针(标注)以及折线的基本使用
- 【iOS】iOS开发中,如何使用百度地图SDK!
- android开发中百度地图的使用
- iOS开发-开发百度地图1-iOS SDK 3.0.0-基本地图的配置及实现
- iOS百度地图开发笔记(二)
- iOS地图开发-基本使用
- iOS开发中使用百度地图计算两点间的距离
- AndroidStudio 中使用 百度地图 <二> 百度地图的使用,个人摘要
- iOS百度地图开发中遇到的问题
- 关键词竞争度越大排名到首页的时间越长
- 病毒木马查杀实战第008篇:熊猫烧香之病毒查杀总结
- 提升你设计水平的CSS3新技术
- 这一世,我注定痴念的老去
- Linux下的C编程实战(一)――开发平台搭建
- ios开发中百度地图的基本使用二
- Linux shell 之比较判断
- Java 读写Properties配置文件
- 弥补Web开发缺陷实战 HTML5 中存储API
- Photoshop脚本 > 改变通道的颜色
- QT创建与调用Dll方法(包括类成员)--显式调用
- Linux内核【链表】整理笔记(1)
- 第13周项目1--数组大折腾&插入移动
- IE兼容问题解决根源:深刻理解hasLayout属性,原理与使用方法