ios百度地图-路径规划

时间:2014-08-26 01:51:25   收藏:0   阅读:470

百度地图的路径规划功能,
在使用百度路径的时候,出现了一些小问题,在此,分享一下自己的最简单的一个路径小demo
当然,前面的百度配置问题,我就不和大家讲了,因为这方面的资料太多了!现在,我来介绍一下这个小demo

bubuko.com,布布扣

AppDelegate.m文件如下,

#import "AppDelegate.h"

import “rootViewController.h”

@implementation AppDelegate

}

然后,rootViewController.m文件如下,这里只有步行这个按钮有效果哦!

#import "rootViewController.h"

import “BMapKit.h”

define MYBUNDLE_NAME @ “mapapi.bundle”

define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]

define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]

@interface RouteAnnotation : BMKPointAnnotation
{

int _type; ///<0:起点 1:终点 2:公交 3:地铁 4:驾乘 5:途经点
int _degree;

}

@property (nonatomic) int type;
@property (nonatomic) int degree;
@end

@implementation RouteAnnotation

@synthesize type = type;
@synthesize degree =
degree;
@end

@interface rootViewController ()
{

UITextField *textFrom;
UITextField *textTo;

UIButton *btnBus;
UIButton *btnCar;
UIButton *btnfoot;
UIView *_mapV ;

BMKMapView* _mapView;
BMKRouteSearch* _routesearch;

}

@end

@implementation UIImage(InternalMethod)

@end

@interface rootViewController ()

@end

@implementation rootViewController

}

-(void)setUpUI
{

UILabel *lableFrom = [[UILabel alloc] initWithFrame:CGRectMake(20,70, 60, 30)];
lableFrom.text = @"起点";

UILabel *lableTo = [[UILabel alloc] initWithFrame:CGRectMake(20,120, 60, 30)];
lableTo.text = @"终点";

textFrom = [[UITextField alloc] initWithFrame:CGRectMake(100, 70, 80, 30)];
textFrom.backgroundColor = [UIColor greenColor];

textTo = [[UITextField alloc] initWithFrame:CGRectMake(100, 120, 80, 30)];
textTo.backgroundColor = [UIColor greenColor];


btnBus = [[UIButton alloc] initWithFrame:CGRectMake(20, 160, 40, 30)];
[btnBus setTitle:@"公交" forState:UIControlStateNormal];
btnCar = [[UIButton alloc] initWithFrame:CGRectMake(80, 160, 40, 30)];
[btnCar setTitle:@"驾车" forState:UIControlStateNormal];
btnfoot = [[UIButton alloc] initWithFrame:CGRectMake(150, 160, 40, 30)];
[btnfoot setTitle:@"步行" forState:UIControlStateNormal];
[btnfoot addTarget:self action:@selector(walkBtnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview: lableFrom];
[self.view addSubview: lableTo];
[self.view addSubview: textFrom];
[self.view addSubview: textTo];
[self.view addSubview: btnBus];
[self.view addSubview: btnCar];
[self.view addSubview: btnfoot];

CGFloat height = [UIScreen mainScreen].bounds.size.height - CGRectGetMaxY(btnBus.frame) ;

_mapV = [[UIView alloc] initWithFrame:CGRectMake(0,   CGRectGetMaxY(btnBus.frame), 320, height)];
_mapV.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_mapV];

}

-(void)walkBtnClick
{

BMKPlanNode* start = [[BMKPlanNode alloc]init] ;
start.name = textFrom.text;
start.cityName = @"北京市";
BMKPlanNode* end = [[BMKPlanNode alloc]init];
end.name = textTo.text;
end.cityName = @"北京市";

BMKWalkingRoutePlanOption *walkingRouteSearchOption = [[BMKWalkingRoutePlanOption alloc]init];
walkingRouteSearchOption.from = start;
walkingRouteSearchOption.to = end;

NSLog(@"%@------%@",start.name,end.name);

BOOL flag = [_routesearch walkingSearch:walkingRouteSearchOption];
if(flag)
{
    NSLog(@"walk检索发送成功");
}
else
{
    NSLog(@"walk检索发送失败");
}

}

}

pragma mark -这里是换大头针的把!

而 _routesearch.delegate = self;在设置代理的时候,使用的是BMKRouteSearch百度api中的代理,而此代理必须先实现了代理方法才能设置代理(一般我们些的代理方法,并不需要先遵守哦!)而且我们使用的是arc,所以,这行代码会报错!必须把代理属性设置为weak才行哦!

其它基本上没有什么错误了哈,其它不会可以参考官方demo哦!

原文:http://my.oschina.net/panyong/blog/306558

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!