iOS中手把手教你集成地圖(干貨,百度地圖為例)

俗話說,好記性不如爛筆頭,關(guān)于集成地圖 (這里以百度地圖為例) 這一塊,本人目前在一家代駕公司做了一年了,對這一塊比較熟悉,現(xiàn)在總結(jié)一下常用方法,希望能幫到有需要的小伙伴。

1.集成地圖環(huán)境
先去百度官方下載SDK,然后導(dǎo)入對應(yīng)的文件到你的項(xiàng)目中,在這里雜亂的不說,提幾個(gè)地方:mapapi.bundle別忘了導(dǎo)入; 除了導(dǎo)入百度提供的包,還要手動(dòng)在程序中添加系統(tǒng)庫; info.plist文件中幾個(gè)操作:iOS9后http協(xié)議的設(shè)置;獲取地理位置的設(shè)置;display name的設(shè)置; 最后一點(diǎn),去百度申請的key要對應(yīng)你項(xiàng)目中的buddle id 。xcode7.3中自動(dòng)提示有時(shí)候挺讓人無語的,不出來我們的結(jié)果,導(dǎo)入頭文件的時(shí)候他提示的都不對,現(xiàn)在把所以頭文件寫在下面,根據(jù)需要復(fù)制粘貼即可。

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關(guān)所有的頭文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的頭文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入檢索功能所有的頭文件
#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云檢索功能所有的頭文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的頭文件
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入計(jì)算工具所有的頭文件
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周邊雷達(dá)功能所有的頭文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的單個(gè)頭文件

好了,第一步結(jié)束。

2.基本地圖的實(shí)現(xiàn)
在appdelegate中導(dǎo)入<BaiduMapAPI_Base/BMKMapManager.h>框架,并服從BMKGeneralDelegate代理,在didFinishLaunchingWithOptions方法中實(shí)現(xiàn)如下代碼

    _mapManager = [[BMKMapManager alloc] init];
    BOOL ret = [_mapManager start:@"你的key"generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    return YES;

在viewcontroller中,遵循BMKMapViewDelegate代理

//遵循代理寫在viewwillappear中
- (void)viewWillAppear:(BOOL)animated {
    [_mapView viewWillAppear];
    _mapView.delegate = self;
    _locService.delegate = self;
    _geoCodeSearch.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [_mapView viewWillDisappear];
    _mapView.delegate = nil;
    _locService.delegate = nil;
    _geoCodeSearch.delegate = nil;
}

在viewdidload中,

    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height - 100)];
    [self.view addSubview:_mapView];

然后地圖出來,到這一步算是剛開始

3.地圖的定位

    UIButton *positionBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    positionBtn.frame = CGRectMake(30, 64, 70, 20);
    [positionBtn setTitle:@"定位" forState:UIControlStateNormal];
    [positionBtn addTarget:self action:@selector(position:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:positionBtn];

遵循BMKLocationServiceDelegate代理,定義BMKLocationService類
在position點(diǎn)擊方法中

    _locService.delegate = self;
    _mapView.zoomLevel = 14.1; //地圖等級(jí),數(shù)字越大越清晰
    _mapView.showsUserLocation = NO;//是否顯示定位小藍(lán)點(diǎn),no不顯示,我們下面要自定義的(這里顯示前提要遵循代理方法,不可缺少)
    _mapView.userTrackingMode = BMKUserTrackingModeNone;
    //定位
    [_locService startUserLocationService];

定位代理方法-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation,在這個(gè)方法中,定位時(shí)候能獲取到經(jīng)緯度 userLocation.location.coordinate
然后點(diǎn)擊了,發(fā)現(xiàn)地圖沒有動(dòng)靜,這是為啥哩~
哦,原來我們忘記設(shè)置了地圖的中心點(diǎn)

_mapView.centerCoordinate = userLocation.location.coordinate(如果直接寫在代理方法中,需要在代理方法末尾調(diào)用[_locService stopUserLocationService] 方法,讓定位停止,要不然一直定位,你的地圖就一直鎖定在一個(gè)位置)。

當(dāng)然了,想要?jiǎng)赢嫷脑挘覀冞€是這樣設(shè)置:

[_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];

然后定位吧,地圖上就顯示出一個(gè)藍(lán)色小點(diǎn),然后你高興了吧,沒等你高興多久,產(chǎn)品跑來給你說,定位的圖片我們需要用我們自己設(shè)計(jì)的圖片,傻了吧。接下來就需要用到自定義標(biāo)注了。

4.標(biāo)注
講標(biāo)注之前,需要弄懂兩個(gè)類的區(qū)別,BMKPointAnnotation和BMKAnnotationView,很多初入地圖行的人都弄不清(我自己一開始也是),前者官方解釋的是一個(gè)點(diǎn)的標(biāo)注,后者則是標(biāo)注視圖,好像還是不清楚0.0。。。。按照我的理解就是:前者代表一個(gè)點(diǎn),比如你地圖上有很多紅色大頭針,大頭針往那里一插,是不是有個(gè)點(diǎn)?這個(gè)點(diǎn),就表示BMKPointAnnotation,具有地理的經(jīng)緯度特性(其他的一些信息也可以寫在這里,如果你的后臺(tái)將一系列信息包括經(jīng)緯度信息傳給你的話,你就可以重寫這個(gè)類)。BMKAnnotationView則代表這個(gè)紅色大頭針,什么?紅色大頭針不好看,我要換成德瑪西亞巨劍,好的,直接在這個(gè)BMKAnnotationView內(nèi)部添加image即可,怎么樣,弄懂了嗎?? 好,下面來代碼,走起~
首先解決第一個(gè)問題,系統(tǒng)藍(lán)色定位小圓點(diǎn)太丑,要換我們自己的,OK。
(1) 對BMKPointAnnotation操作,上面定位獲取地理位置的代理方法中

    _pointAnnotation = [[BMKPointAnnotation alloc] init];
    _pointAnnotation.coordinate = userLocation.location.coordinate;
    _pointAnnotation.title = @"我在這個(gè)地方";
    _pointAnnotation.subtitle = @"你在哪呢";
    [_mapView addAnnotation:_pointAnnotation];
    [_mapView selectAnnotation:_pointAnnotation animated:YES];

[_mapView addAnnotation:_pointAnnotation];,運(yùn)行的時(shí)候他會(huì)自動(dòng)去尋找BMKAnnotationView,這個(gè)需要在標(biāo)注代理方法中寫。

(2) 對BMKAnnotationView操作,新建一個(gè)類繼承于BMKAnnotationView,在.h中聲明一個(gè)屬性

@property (nonatomic, strong) UIImageView *bgImageView;

在.m文件中重寫init方法

- (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        self.centerOffset = CGPointMake(0, 0);
        //定義改標(biāo)注總的大小
        self.frame = CGRectMake(0, 0, 39, 39);
        
        _bgImageView = [[UIImageView alloc] initWithFrame:self.frame];
         _bgImageView.image = [UIImage imageNamed:@"iconsend.png"];
        [self addSubview:_bgImageView];
    }
    return self;
}

(3) 生成對應(yīng)氣泡時(shí)候觸發(fā)的方法

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
//if ([annotation isKindOfClass:[BMKPointAnnotation class]]) //判斷是哪個(gè)BMKPointAnnotation
       MyAnnotionView *newAnnotationView = (MyAnnotionView *)[mapView dequeueReusableAnnotationViewWithIdentifier:myLocationViewID];
        if (newAnnotationView == nil) {
            newAnnotationView = [[MyAnnotionView alloc] initWithAnnotation:annotation reuseIdentifier:myLocationViewID];
        }
        return newAnnotationView;
}

如下圖:

Paste_Image.png

自定義的標(biāo)注完成了,自定義氣泡呢?這涉及到一個(gè)paopaoView,也就是BMKAnnotationView內(nèi)部的一個(gè)屬性,它就是點(diǎn)擊觸發(fā)的氣泡視圖。
在上面的(2)方法中,添加一段paopaoView代碼

- (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        self.centerOffset = CGPointMake(0, 0);
        self.frame = CGRectMake(0, 0, 39, 39);
        
        _bgImageView = [[UIImageView alloc] initWithFrame:self.frame];
                _bgImageView.image = [UIImage imageNamed:@"iconsend.png"];
        [self addSubview:_bgImageView];
        
        UIImageView *paoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        paoView.image =[UIImage imageNamed:@"iconsend.png"];
        self.paopaoView = [[BMKActionPaopaoView alloc] initWithCustomView:paoView];
        
    }
    return self;
}

運(yùn)行,如下

paopao.gif

還有我們什么時(shí)候重寫B(tài)MKPointAnnotation呢,一般來說他只是傳個(gè)地理位置信息,當(dāng)我們在地圖上想要顯示多個(gè)地理位置信息的時(shí)候,比如后臺(tái)返回來一個(gè)數(shù)組,里面每個(gè)元素都是一個(gè)字典,每個(gè)字典代表一個(gè)單位,除了地理位置信息,還有其他信息,比如名字,圖片等,對應(yīng)的是每個(gè)地理位置,這時(shí)候重寫B(tài)MKPointAnnotation,并在其中定義一個(gè)model屬性,用來接收后臺(tái)返回來的model信息串。然后聲明這個(gè)pointAnnotation類的對象,并給他賦值后臺(tái)返回來的model信息串中的位置信息,并將整個(gè)model傳給他,后面的操作和上面的步驟一樣。
具體想怎么做,看你自己~~~

5.POI檢索
我們先定義一個(gè)輸入框,然后根據(jù)輸入的文字來大概的地址

UITextField *poiTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 30, 100, 20)];
    poiTextField.backgroundColor = [UIColor lightGrayColor];
    [poiTextField addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventEditingChanged];
    poiTextField.delegate = self;
    [self.view addSubview:poiTextField];
- (void)valueChange:(UITextField *)textField {
    NSLog(@"123");
    
    _poiSearch = [[BMKPoiSearch alloc] init];
    _poiSearch.delegate = self;
    NSLog(@"搜索:%@",textField.text);
    //附近云檢索
    BMKNearbySearchOption *nearBySearchOption = [[BMKNearbySearchOption alloc] init];
    nearBySearchOption.pageIndex = 0;
    nearBySearchOption.pageCapacity = 10;
    nearBySearchOption.keyword = textField.text;
    
    //檢索的中心點(diǎn)
    nearBySearchOption.location = _locService.userLocation.location.coordinate;
    nearBySearchOption.radius = 100;
    
    BOOL flag = [_poiSearch poiSearchNearBy:nearBySearchOption];
    if (flag) {
        NSLog(@"success");
    } else {
        NSLog(@"fail");
    }
}

在返回的代理方法中,我們打印一下

- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode {
    if (errorCode == BMK_SEARCH_NO_ERROR) {
        for (int i = 0; i < poiResult.poiInfoList.count; i++) {
            BMKPoiInfo *info = [poiResult.poiInfoList objectAtIndex:i];
            NSLog(@"地址:%@", info.name);
        }
    }
}

效果圖如下

poi.gif

是不是得到我們要的數(shù)據(jù)了呢。然后把這些數(shù)據(jù)顯示到tableview上,是不是就像我們常見的搜索框搜索,得到對應(yīng)的結(jié)果了~~~

6.地理反編碼
地理反編碼的應(yīng)用一般是移動(dòng)地圖,然后顯示附近的地理信息(我們常見的app上這個(gè)功能一般是和poi搜索配合使用的)
聲明變量,遵循代理

@property (nonatomic, strong) BMKGeoCodeSearch *geoCodeSearch;

移動(dòng)地圖的代理方法

- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

        CLLocationCoordinate2D carLocation = [_mapView convertPoint:self.view.center toCoordinateFromView:self.view];
        BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc] init];
        option.reverseGeoPoint = CLLocationCoordinate2DMake(carLocation.latitude, carLocation.longitude);
        NSLog(@"%f - %f", option.reverseGeoPoint.latitude, option.reverseGeoPoint.longitude);
        //調(diào)用發(fā)地址編碼方法,讓其在代理方法onGetReverseGeoCodeResult中輸出
        [_geoCodeSearch reverseGeoCode:option];
}
//返回地理反編碼
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
    if (result) {
        NSLog(@"%@ - %@ - %@ - %@ - %@", result.addressDetail.province, result.addressDetail.city, result.addressDetail.streetName, result.address, result.businessCircle);  
    } else {
        NSLog(@"找不到");
    }
}

移動(dòng)地圖,效果如下:

geo.gif

當(dāng)然了,如果你想配合著poi使用,只需要把result.poiList數(shù)據(jù)源存儲(chǔ)到數(shù)組中,然后在tableView中顯示出來,你會(huì)了嗎?~~~~

7.路徑規(guī)劃
將目的地和初始地點(diǎn)連接起來,通過百度內(nèi)部API來告訴我們怎么去(也就是去一個(gè)地方查地圖一樣)
首先導(dǎo)入<BaiduMapAPI_Search/BMKRouteSearch.h>
<BaiduMapAPI_Utils/BMKUtilsComponent.h>這兩個(gè)頭文件,并遵循代理BMKRouteSearchDelegate。

在.m文件最上面,我們還要聲明一個(gè)PointAnnotation類,代碼如下

@interface RouteAnnotation : BMKPointAnnotation
{
    int _type; //0:起點(diǎn) 1:終點(diǎn) 2:公交 3:地鐵 4:駕乘 5:途經(jīng)點(diǎn)
    int _degree;
}

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

@implementation RouteAnnotation

@synthesize type = _type;
@synthesize degree = _degree;

@end

同時(shí),我們定義一個(gè)屬性

@property (nonatomic, strong) BMKRouteSearch *routeSearch;

在button點(diǎn)擊方法里實(shí)現(xiàn)下面的代碼

- (void)PlanBtn:(UIButton *)btn {
    _routeSearch = [[BMKRouteSearch alloc] init];
    _routeSearch.delegate = self;
    
    //發(fā)起檢索
    BMKPlanNode *start = [[BMKPlanNode alloc] init];
    start.name = @"國貿(mào)";
    BMKPlanNode *end = [[BMKPlanNode alloc] init];
    end.name = @"國家體育總局";
    
    BMKTransitRoutePlanOption *transiRouteS = [[BMKTransitRoutePlanOption alloc] init];
    transiRouteS.city = @"北京市";
    transiRouteS.from = start;
    transiRouteS.to = end;
    
    BOOL flag = [_routeSearch transitSearch:transiRouteS];
    if (flag) {
        NSLog(@"transtion檢索發(fā)送成功");
    } else {
        NSLog(@"fail");
    }
}

在上面的代碼中,我們用的是BMKTransitRoutePlanOption,也就是公交(地鐵)方式,如果你想用別的(比如步行,或者騎乘,駕車等),可在這替換方法

由于上面我們用的是公交,所以下面我們要實(shí)現(xiàn)公交的代理方法(每種方式有每種方式的代理方法)

- (void)onGetTransitRouteResult:(BMKRouteSearch *)searcher result:(BMKTransitRouteResult *)result errorCode:(BMKSearchErrorCode)error {
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此處理正常結(jié)果
        BMKTransitRouteLine* plan = (BMKTransitRouteLine*)[result.routes objectAtIndex:0];
        NSInteger size = [plan.steps count];
        NSLog(@"size == %ld", (long)size);
        int planPointCounts = 0;
        for (int i = 0; i < size; i++) {
            BMKTransitStep *tansitStep = [plan.steps objectAtIndex:i];
            if (i == 0 ) {
                RouteAnnotation *item = [[RouteAnnotation alloc] init];
                item.coordinate = plan.starting.location;
                item.title = @"起點(diǎn)";
                item.type = 0;
                [_mapView addAnnotation:item]; //添加起點(diǎn)標(biāo)注
            } else if (i == size - 1) {
                RouteAnnotation *item = [[RouteAnnotation alloc] init];
                item.coordinate = plan.terminal.location;
                item.title = @"終點(diǎn)";
                item.type = 1;
                [_mapView addAnnotation:item];
            }
            RouteAnnotation *item = [[RouteAnnotation alloc] init];
            item.coordinate = tansitStep.entrace.location; //路段入口信息
            item.title = tansitStep.instruction; //路程換成說明
            item.type = 3;
            [_mapView addAnnotation:item];
            
            //軌跡點(diǎn)總數(shù)累計(jì)
            planPointCounts += tansitStep.pointsCount;
        }
        
        //軌跡點(diǎn)
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts]; //文件后綴名改為mm
        int i = 0;
        for (int j = 0; j < size; j++) {
            BMKTransitStep *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構(gòu)建BMKPolyline
        BMKPolyline *polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
        [_mapView addOverlay:polyLine]; //添加路線overlay
        delete []temppoints;
        [self mapViewFitPolyLine:polyLine];
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
        //當(dāng)路線起終點(diǎn)有歧義時(shí)通,獲取建議檢索起終點(diǎn)
        //result.routeAddrResult
    }
    else {
        NSLog(@"抱歉,未找到結(jié)果");
    }
}

在上面結(jié)尾那里我們添加了路線overlay,所以我們要實(shí)現(xiàn)overlay的代理方法

- (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay
{
    if ([overlay isKindOfClass:[BMKPolyline class]]) {
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:1];
        polylineView.strokeColor = [[UIColor alloc] initWithRed:0 green:0 blue:1 alpha:0.7];
        polylineView.lineWidth = 3.0;
        return polylineView;
    }
    return nil;
}

在這里,你可以根據(jù)自己的需要改變線條的一些屬性,不多說。
把下面這段代碼復(fù)制到工程里,這是百度給我們寫好的根據(jù)路徑計(jì)算地圖范圍的,挺棒的,直接用他們的。

/根據(jù)polyline設(shè)置地圖范圍
- (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 = 1; 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;
}

由于我們在公交的代理方法中添加了標(biāo)注,所以我們要在標(biāo)注的代理方法中實(shí)現(xiàn)我們自定義的標(biāo)注

if ([annotation isKindOfClass:[RouteAnnotation class]]) {
        return [self getRouteAnnotationView:mapView viewForAnnotation:annotation];
    }

這里面調(diào)用了一個(gè)方法,我們用百度SDK中給我們寫好的,由于他們寫的較多,還涉及到其他文件,在這里我們就舉個(gè)例子,所以有的我給注掉了,咱們就看個(gè)顯示效果,好看點(diǎn)的效果到時(shí)候咱們再去百度SDK中把它的拿來。

- (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 imageNamed:@"icon_nav_start.png"];
                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 imageNamed:@"icon_nav_end.png"];
                view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
                view.canShowCallout = TRUE;
            }
            view.annotation = routeAnnotation;
        }
            break;
        case 2:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"bus_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"bus_node"];
                view.image = [UIImage imageNamed:@"icon_nav_bus.png"];
                view.canShowCallout = TRUE;
            }
            view.annotation = routeAnnotation;
        }
            break;
        case 3:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"rail_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"rail_node"];
                view.image = [UIImage imageNamed:@"icon_nav_rail.png"];
                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 imageNamed:@"icon_direction.png"];
//            view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
            view.annotation = routeAnnotation;
            
        }
            break;
        case 5:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"waypoint_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"waypoint_node"];
                view.canShowCallout = TRUE;
            } else {
                [view setNeedsDisplay];
            }
            
//            UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_waypoint.png"]];
//            view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
            view.annotation = routeAnnotation;
        }
            break;
        default:
            break;
    }
    
    return view;
}

好了,到這里就算結(jié)束了,根據(jù)公交進(jìn)行路徑規(guī)劃,實(shí)現(xiàn)效果如下:

bus.gif

對了,如果你想算出開始到結(jié)束的距離(非直線距離,一般我們都算走過的里程),可以直接在公交的代理方法中,用BMKTransitRouteLine的distance屬性,再除以1000,就可以算出公里數(shù)了,很簡單吧??~~~

        BMKTransitRouteLine* plan = (BMKTransitRouteLine*)[result.routes objectAtIndex:0];
        NSLog(@"juli is == %d公里", plan.distance / 1000);

打印結(jié)果如下圖:

Paste_Image.png

當(dāng)然了,你項(xiàng)目中想實(shí)現(xiàn)別的種類,比如駕車(會(huì)有提示到哪拐彎之類的),你可以自己根據(jù)百度SDK中寫的改一改(這個(gè)路徑規(guī)劃還是看一看百度SDK吧,我在這不多寫了,畢竟方式都一樣,只是種類多)。

斷斷續(xù)續(xù)寫了好幾天,挺多的,也希望能幫到不怎么會(huì)地圖集成的小伙伴們~
更新:有的小伙伴需要demo,我上傳了github上,地址:https://github.com/Feijunjie/BaiduMapDemo/tree/master
所以,如果你覺得我寫的不錯(cuò)或者幫到您了,就給我點(diǎn)個(gè)贊吧

2016-10-10更新

關(guān)于根據(jù)地圖上大頭針的分布范圍,來動(dòng)態(tài)縮放地圖,寫在這里了,需要的小伙伴可以看看 http://www.lxweimin.com/p/2fb973092892

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,517評(píng)論 6 539
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,087評(píng)論 3 423
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,521評(píng)論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,493評(píng)論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,207評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,603評(píng)論 1 325
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,624評(píng)論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,813評(píng)論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,364評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,110評(píng)論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,305評(píng)論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,874評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,532評(píng)論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,953評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,209評(píng)論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,033評(píng)論 3 396
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,268評(píng)論 2 375

推薦閱讀更多精彩內(nèi)容