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

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

1.集成地圖環(huán)境
先去百度官方下載SDK,然后導入對應的文件到你的項目中邀泉,在這里雜亂的不說嬉挡,提幾個地方:mapapi.bundle別忘了導入; 除了導入百度提供的包汇恤,還要手動在程序中添加系統(tǒng)庫庞钢; info.plist文件中幾個操作:iOS9后http協(xié)議的設置;獲取地理位置的設置因谎;display name的設置基括; 最后一點,去百度申請的key要對應你項目中的buddle id 财岔。xcode7.3中自動提示有時候挺讓人無語的风皿,不出來我們的結果饭冬,導入頭文件的時候他提示的都不對,現在把所以頭文件寫在下面揪阶,根據需要復制粘貼即可昌抠。

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關所有的頭文件
#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>//引入計算工具所有的頭文件
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周邊雷達功能所有的頭文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的單個頭文件

好了,第一步結束鲁僚。

2.基本地圖的實現
在appdelegate中導入<BaiduMapAPI_Base/BMKMapManager.h>框架炊苫,并服從BMKGeneralDelegate代理,在didFinishLaunchingWithOptions方法中實現如下代碼

    _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點擊方法中

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

定位代理方法-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation,在這個方法中盖灸,定位時候能獲取到經緯度 userLocation.location.coordinate
然后點擊了蚁鳖,發(fā)現地圖沒有動靜,這是為啥哩~
哦赁炎,原來我們忘記設置了地圖的中心點

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

當然了姻报,想要動畫的話己英,我們還是這樣設置:

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

然后定位吧,地圖上就顯示出一個藍色小點逗抑,然后你高興了吧剧辐,沒等你高興多久,產品跑來給你說邮府,定位的圖片我們需要用我們自己設計的圖片荧关,傻了吧。接下來就需要用到自定義標注了褂傀。

4.標注
講標注之前忍啤,需要弄懂兩個類的區(qū)別,BMKPointAnnotation和BMKAnnotationView,很多初入地圖行的人都弄不清(我自己一開始也是)同波,前者官方解釋的是一個點的標注鳄梅,后者則是標注視圖,好像還是不清楚0.0未檩。戴尸。。冤狡。按照我的理解就是:前者代表一個點孙蒙,比如你地圖上有很多紅色大頭針,大頭針往那里一插悲雳,是不是有個點挎峦?這個點,就表示BMKPointAnnotation合瓢,具有地理的經緯度特性(其他的一些信息也可以寫在這里坦胶,如果你的后臺將一系列信息包括經緯度信息傳給你的話,你就可以重寫這個類)晴楔。BMKAnnotationView則代表這個紅色大頭針顿苇,什么?紅色大頭針不好看滥崩,我要換成德瑪西亞巨劍岖圈,好的,直接在這個BMKAnnotationView內部添加image即可钙皮,怎么樣,弄懂了嗎顽决?短条? 好,下面來代碼才菠,走起~
首先解決第一個問題茸时,系統(tǒng)藍色定位小圓點太丑,要換我們自己的赋访,OK可都。
(1) 對BMKPointAnnotation操作,上面定位獲取地理位置的代理方法中

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

[_mapView addAnnotation:_pointAnnotation];蚓耽,運行的時候他會自動去尋找BMKAnnotationView渠牲,這個需要在標注代理方法中寫。

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

@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);
        //定義改標注總的大小
        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) 生成對應氣泡時候觸發(fā)的方法

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

如下圖:

Paste_Image.png

自定義的標注完成了,自定義氣泡呢鼎兽?這涉及到一個paopaoView答姥,也就是BMKAnnotationView內部的一個屬性铣除,它就是點擊觸發(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;
}

運行尚粘,如下

paopao.gif

還有我們什么時候重寫B(tài)MKPointAnnotation呢,一般來說他只是傳個地理位置信息敲长,當我們在地圖上想要顯示多個地理位置信息的時候郎嫁,比如后臺返回來一個數組,里面每個元素都是一個字典潘明,每個字典代表一個單位行剂,除了地理位置信息,還有其他信息钳降,比如名字厚宰,圖片等,對應的是每個地理位置遂填,這時候重寫B(tài)MKPointAnnotation铲觉,并在其中定義一個model屬性,用來接收后臺返回來的model信息串吓坚。然后聲明這個pointAnnotation類的對象撵幽,并給他賦值后臺返回來的model信息串中的位置信息,并將整個model傳給他礁击,后面的操作和上面的步驟一樣盐杂。
具體想怎么做,看你自己~~~

5.POI檢索
我們先定義一個輸入框哆窿,然后根據輸入的文字來大概的地址

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;
    
    //檢索的中心點
    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

是不是得到我們要的數據了呢。然后把這些數據顯示到tableview上挚躯,是不是就像我們常見的搜索框搜索强衡,得到對應的結果了~~~

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

@property (nonatomic, strong) BMKGeoCodeSearch *geoCodeSearch;

移動地圖的代理方法

- (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);
        //調用發(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(@"找不到");
    }
}

移動地圖,效果如下:

geo.gif

當然了缩搅,如果你想配合著poi使用越败,只需要把result.poiList數據源存儲到數組中,然后在tableView中顯示出來誉己,你會了嗎眉尸?~~~~

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

在.m文件最上面霉祸,我們還要聲明一個PointAnnotation類,代碼如下

@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

同時袱蜡,我們定義一個屬性

@property (nonatomic, strong) BMKRouteSearch *routeSearch;

在button點擊方法里實現下面的代碼

- (void)PlanBtn:(UIButton *)btn {
    _routeSearch = [[BMKRouteSearch alloc] init];
    _routeSearch.delegate = self;
    
    //發(fā)起檢索
    BMKPlanNode *start = [[BMKPlanNode alloc] init];
    start.name = @"國貿";
    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,也就是公交(地鐵)方式坪蚁,如果你想用別的(比如步行奔穿,或者騎乘,駕車等)敏晤,可在這替換方法

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

- (void)onGetTransitRouteResult:(BMKRouteSearch *)searcher result:(BMKTransitRouteResult *)result errorCode:(BMKSearchErrorCode)error {
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此處理正常結果
        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 = @"起點";
                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];
            }
            RouteAnnotation *item = [[RouteAnnotation alloc] init];
            item.coordinate = tansitStep.entrace.location; //路段入口信息
            item.title = tansitStep.instruction; //路程換成說明
            item.type = 3;
            [_mapView addAnnotation:item];
            
            //軌跡點總數累計
            planPointCounts += tansitStep.pointsCount;
        }
        
        //軌跡點
        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構建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){
        //當路線起終點有歧義時通,獲取建議檢索起終點
        //result.routeAddrResult
    }
    else {
        NSLog(@"抱歉嘴脾,未找到結果");
    }
}

在上面結尾那里我們添加了路線overlay男摧,所以我們要實現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;
}

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

/根據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 = 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;
}

由于我們在公交的代理方法中添加了標注韵洋,所以我們要在標注的代理方法中實現我們自定義的標注

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

這里面調用了一個方法竿刁,我們用百度SDK中給我們寫好的,由于他們寫的較多搪缨,還涉及到其他文件们妥,在這里我們就舉個例子,所以有的我給注掉了勉吻,咱們就看個顯示效果,好看點的效果到時候咱們再去百度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;
}

好了齿桃,到這里就算結束了,根據公交進行路徑規(guī)劃煮盼,實現效果如下:

bus.gif

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

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

打印結果如下圖:

Paste_Image.png

當然了,你項目中想實現別的種類梗脾,比如駕車(會有提示到哪拐彎之類的)荸型,你可以自己根據百度SDK中寫的改一改(這個路徑規(guī)劃還是看一看百度SDK吧,我在這不多寫了炸茧,畢竟方式都一樣瑞妇,只是種類多)。

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

2016-10-10更新

關于根據地圖上大頭針的分布范圍润脸,來動態(tài)縮放地圖柬脸,寫在這里了,需要的小伙伴可以看看 http://www.reibang.com/p/2fb973092892

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末毙驯,一起剝皮案震驚了整個濱河市倒堕,隨后出現的幾起案子,更是在濱河造成了極大的恐慌爆价,老刑警劉巖垦巴,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異铭段,居然都是意外死亡骤宣,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進店門序愚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來憔披,“玉大人,你說我怎么就攤上這事爸吮》蚁ィ” “怎么了?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵形娇,是天一觀的道長锰霜。 經常有香客問我,道長桐早,這世上最難降的妖魔是什么癣缅? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任厨剪,我火速辦了婚禮,結果婚禮上友存,老公的妹妹穿的比我還像新娘祷膳。我一直安慰自己,他們只是感情好爬立,可當我...
    茶點故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布钾唬。 她就那樣靜靜地躺著,像睡著了一般侠驯。 火紅的嫁衣襯著肌膚如雪抡秆。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天吟策,我揣著相機與錄音儒士,去河邊找鬼。 笑死檩坚,一個胖子當著我的面吹牛着撩,可吹牛的內容都是我干的。 我是一名探鬼主播匾委,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼拖叙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了赂乐?” 一聲冷哼從身側響起薯鳍,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎挨措,沒想到半個月后挖滤,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡浅役,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年斩松,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片觉既。...
    茶點故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡惧盹,死狀恐怖,靈堂內的尸體忽然破棺而出瞪讼,到底是詐尸還是另有隱情岭参,我是刑警寧澤,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布尝艘,位于F島的核電站,受9級特大地震影響姿染,放射性物質發(fā)生泄漏背亥。R本人自食惡果不足惜秒际,卻給世界環(huán)境...
    茶點故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望狡汉。 院中可真熱鬧娄徊,春花似錦、人聲如沸盾戴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽尖啡。三九已至橄仆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間衅斩,已是汗流浹背盆顾。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留畏梆,地道東北人您宪。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像奠涌,于是被迫代替她去往敵國和親宪巨。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,619評論 2 354

推薦閱讀更多精彩內容