iOS百度地图开发之路径规划

路线规划

示例程序

使用百度地图SDK版本为2.9.1。
需要将改

代码:

#import <MapKit/MapKit.h>
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Map/BMKPolylineView.h>
#import <BaiduMapAPI_Utils/BMKGeometry.h>
#import "HXRouteViewController.h"
#import "UIView+Extension.h"
#import "HXAnnotationView.h"
#import "HXAnnotation.h"
#import "HXMenDDetail.h"
#import "UIImage+Rotate.h"
#import "UIColor+HexString.h"
#import "FMActionSheet.h"
#import "ZoomInOrOutButton.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 HXRouteViewController ()<BMKMapViewDelegate, BMKRouteSearchDelegate>{
    BMKMapView *_mapView;
    NSString *_cityName;
    BMKRouteSearch *_routesearch;
}

/**
 *  返回按钮
 */
//@property (nonatomic , weak)UIButton *preBtn;

/**
 *  存放标注数组
 */
@property(nonatomic , strong) NSMutableArray *annotations;

/**
 *  用户当前位置
 */
@property(nonatomic , strong) BMKUserLocation *userLocation;

@property(nonatomic , strong) HXAnnotation *anno;

@end

@implementation HXRouteViewController

- (NSString*)getMyBundlePath1:(NSString *)filename
{
    
    NSBundle * libBundle = MYBUNDLE ;
    if ( libBundle && filename ){
        NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent:filename];
        return s;
    }
    return nil ;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 20)];
    statusView.backgroundColor = [UIColor colorWithR:46 G:139 B:84 alpha:1.];
    [self.view addSubview:statusView];
    
    _routesearch = [[BMKRouteSearch alloc] init];
    _routesearch.delegate = self;
    
    //初始化BMKMapView并设置代理
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 20, SCREENW, SCREENH - 140)];
    [self.view addSubview:_mapView];
    _mapView.delegate = self;
    
    _mapView.buildingsEnabled = YES;
    _mapView.overlookEnabled = YES;
    _mapView.showMapScaleBar = YES;
    _mapView.overlooking = -45;
    
    //设置放大/缩小按钮
    [self setupNavButton];
    //返回按钮
    [self backButton];
    //设置导航按钮
//    [self setupNavButton];
    //驾车路线
    [self showDriveSearch];
    //设置放大/缩小按钮
    [self setupZoomButton];
}

#pragma mark 设置放大/缩小按钮
-(void)setupZoomButton{
    ZoomInOrOutButton *zoomInOrOutBtn = [[[NSBundle mainBundle] loadNibNamed:@"ZoomInOrOutButton" owner:nil options:nil] lastObject];
    zoomInOrOutBtn.layer.cornerRadius = 5;
    
    zoomInOrOutBtn.x = SCREENW - 110;
    zoomInOrOutBtn.y = _mapView.height - 50;
    [_mapView addSubview:zoomInOrOutBtn];
    [zoomInOrOutBtn.zoomInBtn addTarget:self action:@selector(zoomInBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    zoomInOrOutBtn.zoomInBtn.tag = 0;
    [zoomInOrOutBtn.zoomOutBtn addTarget:self action:@selector(zoomInBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    zoomInOrOutBtn.zoomInBtn.tag = 1;
}

#pragma mark 点击放大按钮
-(void)zoomInBtnClick:(UIButton *)sender{
    _mapView.zoomLevel = sender.tag ? _mapView.zoomLevel++ : _mapView.zoomLevel--;
}

#pragma mark 返回按钮
-(void)backButton{
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 30, 30, 40)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    backButton.backgroundColor = [UIColor blackColor];
    backButton.layer.cornerRadius = 5;
    backButton.alpha = 0.7;
    [backButton bringSubviewToFront:_mapView];
    [backButton initSpringTouch];
    [self.view addSubview:backButton];
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 返回
-(void)goBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark 导航按钮
-(void)setupNavButton{
    
    UIView *bgView = [[UIView alloc] init];
    bgView.frame = CGRectMake(0, SCREENH - 120, SCREENW, 120);
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    CGFloat viewX = 15;
    
    UILabel *shopLabel = [[UILabel alloc] initWithFrame:CGRectMake(viewX, 5, SCREENW - 30, 25)];
    shopLabel.text = [NSString stringWithFormat:@"门店名称:%@",self.mendDetail.shopName];
    shopLabel.textColor = [UIColor blackColor];
    shopLabel.font = [UIFont systemFontOfSize:15];
    [bgView addSubview:shopLabel];
    
    UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(viewX, 30, SCREENW - 30, 25)];
    addressLabel.text = [NSString stringWithFormat:@"门店地址:%@",self.mendDetail.address];
    addressLabel.textColor = [UIColor lightGrayColor];
    addressLabel.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:addressLabel];
    
    UIButton *navButton = [[UIButton alloc] init];
    navButton.frame = CGRectMake(15, 65, SCREENW - 30, 40);
    [navButton setTitle:@"导航" forState:UIControlStateNormal];
    [navButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [navButton.layer setMasksToBounds:YES];
    [navButton.layer setCornerRadius:6];
    [navButton.layer setBorderWidth:1.0];
    [navButton initSpringTouch];
    //设置按钮的边界颜色
    CGColorRef color = [UIColor colorWithR:46 G:139 B:84 alpha:1.].CGColor;
    [navButton.layer setBorderColor:color];
    [navButton addTarget:self action:@selector(navButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:navButton];
}

#pragma mark弹出选择框
-(void)navButtonClick{
    //弹出框
    FMActionSheet *sheet = [[FMActionSheet alloc] initWithTitle:@"导航方式" buttonTitles:[NSArray arrayWithObjects:@"用手机自带地图导航", @"百度地图", nil] cancelButtonTitle:@"取消" delegate:(id<FMActionSheetDelegate>)self];
    sheet.tag = 103;
    sheet.titleFont = [UIFont systemFontOfSize:15];
    sheet.titleBackgroundColor = [UIColor colorWithHexString:@"f4f5f8"];
    sheet.titleColor = [UIColor colorWithHexString:@"666666"];
    sheet.lineColor = [UIColor colorWithHexString:@"dbdce4"];
    [sheet show];
    
}

- (void)actionSheet:(FMActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        //打开系统自带的地图
        [self openIOSMapNav];
    } else if (buttonIndex == 1) {
        //打开百度地图客户端导航
        [self opsenBaiduMap];
    }
}

#pragma mark 打开系统自带的地图
-(void)openIOSMapNav{
    CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake([self.mendDetail.dimensions doubleValue], [self.mendDetail.longitude doubleValue]);
    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];
    toLocation.name = self.mendDetail.shopName;
    [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}

-(void)opsenBaiduMap{
    double latitude =self.coordinate.latitude;
    double longitude =self.coordinate.longitude;
    NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=%@,%@&mode=driving&region=北京",latitude,longitude,self.mendDetail.dimensions,self.mendDetail.longitude];
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *mapUrl = [NSURL URLWithString:urlString];
    if ([[UIApplication sharedApplication] canOpenURL:mapUrl]) {
        [[UIApplication sharedApplication] openURL:mapUrl];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的手机没有安装百度地图" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
    
}

- (UIColor *)actionSheet:(FMActionSheet *)actionSheet buttonTextColorAtIndex:(NSInteger)bottonIndex{
    UIColor *textColor = [UIColor colorWithR:41 G:140 B:82 alpha:1.];
    return textColor;
}

#pragma mark -显示大头针
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation{
    
    if (![annotation isKindOfClass:[RouteAnnotation class]]) return nil;
    return [self getRouteAnnotationView:mapView viewForAnnotation:(RouteAnnotation *)annotation];
}

#pragma mark 获取路线的标注,显示到地图
- (BMKAnnotationView*)getRouteAnnotationView:(BMKMapView *)mapview viewForAnnotation:(RouteAnnotation*)routeAnnotation{
    
    BMKAnnotationView *view = nil;
    switch (routeAnnotation.type) {
        case 0:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"start_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc] initWithAnnotation:routeAnnotation reuseIdentifier:@"start_node"];
                view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_start"]];
                view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
                view.canShowCallout = true;
            }
            view.annotation = routeAnnotation;
        }
            break;
        case 1:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"end_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc] initWithAnnotation:routeAnnotation reuseIdentifier:@"end_node"];
                view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_end"]];
                view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
                view.canShowCallout = true;
            }
            view.annotation =routeAnnotation;
        }
            break;
        case 4:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"route_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc] initWithAnnotation:routeAnnotation reuseIdentifier:@"route_node"];
                view.canShowCallout = true;
            } else {
                [view setNeedsDisplay];
            }
            UIImage *image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_direction"]];
            view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
            view.annotation = routeAnnotation;
        }
            break;
        default:
            break;
    }
    return view;
}

#pragma mark 驾车路线
-(void)showDriveSearch{
    //线路检索节点信息
    BMKPlanNode *start = [[BMKPlanNode alloc] init];
    start.pt = self.coordinate;
    start.cityName = @"北京市";
    BMKPlanNode *end = [[BMKPlanNode alloc] init];
    CLLocationCoordinate2D endCoordinate = CLLocationCoordinate2DMake([self.mendDetail.dimensions doubleValue], [self.mendDetail.longitude doubleValue]);
    end.pt = endCoordinate;
    end.cityName = @"北京市";
    BMKDrivingRoutePlanOption *drivingRouteSearchOption = [[BMKDrivingRoutePlanOption alloc] init];
    drivingRouteSearchOption.from = start;
    drivingRouteSearchOption.to = end;
    BOOL flag = [_routesearch drivingSearch:drivingRouteSearchOption];
    if (flag) {
    }
}

#pragma mark 返回驾乘搜索结果
- (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == BMK_SEARCH_NO_ERROR) {
        //表示一条驾车路线
        BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[result.routes objectAtIndex:0];
        // 计算路线方案中的路段数目
        int size = (int)[plan.steps count];
        int planPointCounts = 0;
        for (int i = 0; i < size; i++) {
            //表示驾车路线中的一个路段
            BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i];
            if(i==0){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.starting.location;
                item.title = @"起点";
                item.type = 0;
                [_mapView addAnnotation:item]; // 添加起点标注
                
            }else if(i==size-1){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.terminal.location;
                item.title = @"终点";
                item.type = 1;
                [_mapView addAnnotation:item]; // 添加终点标注
            }
            //添加annotation节点
            RouteAnnotation* item = [[RouteAnnotation alloc]init];
            item.coordinate = transitStep.entrace.location;
            item.title = transitStep.entraceInstruction;
            item.degree = transitStep.direction * 30;
            item.type = 4;
            [_mapView addAnnotation:item];
            
            //轨迹点总数累计
            planPointCounts += transitStep.pointsCount;
        }
        // 添加途经点
        if (plan.wayPoints) {
            for (BMKPlanNode* tempNode in plan.wayPoints) {
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = tempNode.pt;
                item.type = 5;
                item.title = tempNode.name;
                [_mapView addAnnotation:item];
            }
        }
        //轨迹点
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
        int i = 0;
        for (int j = 0; j < size; j++) {
            BMKDrivingStep* transitStep = [plan.steps objectAtIndex:j];
            int k=0;
            for(k=0;k<transitStep.pointsCount;k++) {
                temppoints[i].x = transitStep.points[k].x;
                temppoints[i].y = transitStep.points[k].y;
                i++;
            }
            
        }
        // 通过points构建BMKPolyline
        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
        [_mapView addOverlay:polyLine]; // 添加路线overlay
        delete []temppoints;
        [self mapViewFitPolyLine:polyLine];
    }
}

#pragma mark 根据overlay生成对应的View
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay{
    if ([overlay isKindOfClass:[BMKPolyline class]]) {
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:1];
        polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        polylineView.lineWidth = 3.0;
        return polylineView;
    }
    return nil;
}

#pragma mark  根据polyline设置地图范围
- (void)mapViewFitPolyLine:(BMKPolyline *) polyLine {
    CGFloat ltX, ltY, rbX, rbY;
    
    if (polyLine.pointCount < 1) return;
    BMKMapPoint pt = polyLine.points[0];
    ltX = pt.x, ltY = pt.y;
    rbX = pt.x, rbY = pt.y;
    
    for (int i = 0; i < polyLine.pointCount; i++) {
        BMKMapPoint pt = polyLine.points[i];
        if (pt.x < ltX) {
            ltX = pt.x;
        }
        if (pt.x > rbX) {
            rbX = pt.x;
        }
        if (pt.y > ltY) {
            ltY = pt.y;
        }
        if (pt.y < rbY) {
            rbY = pt.y;
        }
    }
    BMKMapRect rect;
    rect.origin = BMKMapPointMake(ltX , ltY);
    rect.size = BMKMapSizeMake(rbX - ltX, rbY - ltY);
    [_mapView setVisibleMapRect:rect];
    _mapView.zoomLevel = _mapView.zoomLevel - 0.3;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时,置nil
    _routesearch.delegate = nil;
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,426评论 25 707
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,594评论 18 139
  • 各位小伙伴们大家好,今天我向大家介绍一下苹果百度地图的使用方法,因为做过一些想关的APP,感觉百度地图还是挺方便的...
    Lee0528阅读 14,652评论 18 46
  • 文/雪诺 微信公众号:迷茫人生路 温暖是一个让你忘记烦恼的习惯 似乎已经习惯了每天和老妈的视频聊天了...
    Snow凤阅读 445评论 0 2
  • 感赏儿子渡过愉快的一天,睡到自然醒,自己去买自己想吃的食物,锻炼处事能力,出去玩耍,锻炼身体,回来很开心。玩...
    金色阳光魏艳春阅读 180评论 0 0