俗話說,好記性不如爛筆頭,關(guān)于集成地圖 (這里以百度地圖為例) 這一塊,本人目前在一家代駕公司做了一年了,對這一塊比較熟悉,現(xiàn)在總結(jié)一下常用方法,希望能幫到有需要的小伙伴。
1.集成地圖環(huán)境 先去百度官方下載SDK,然后導(dǎo)入對應(yīng)的文件到你的項目中,在這里雜亂的不說,提幾個地方:mapapi.bundle別忘了導(dǎo)入; 除了導(dǎo)入百度提供的包,還要手動在程序中添加系統(tǒng)庫; info.plist文件中幾個操作:iOS9后http協(xié)議的設(shè)置;獲取地理位置的設(shè)置;display name的設(shè)置; 最后一點(diǎn),去百度申請的key要對應(yīng)你項目中的buddle id 。xcode7.3中自動提示有時候挺讓人無語的,不出來我們的結(jié)果,導(dǎo)入頭文件的時候他提示的都不對,現(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>//引入計算工具所有的頭文件
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周邊雷達(dá)功能所有的頭文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的單個頭文件
好了,第一步結(jié)束。
2.基本地圖的實現(xiàn) 在appdelegate中導(dǎo)入<BaiduMapAPI_Base/BMKMapManager.h>框架,并服從BMKGeneralDelegate代理,在didFinishLaunchingWithOptions方法中實現(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; //地圖等級,數(shù)字越大越清晰
_mapView.showsUserLocation = NO;//是否顯示定位小藍(lán)點(diǎn),no不顯示,我們下面要自定義的(這里顯示前提要遵循代理方法,不可缺少)
_mapView.userTrackingMode = BMKUserTrackingModeNone;
//定位
[_locService startUserLocationService];
定位代理方法-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation,在這個方法中,定位時候能獲取到經(jīng)緯度 userLocation.location.coordinate 然后點(diǎn)擊了,發(fā)現(xiàn)地圖沒有動靜,這是為啥哩~ 哦,原來我們忘記設(shè)置了地圖的中心點(diǎn)
_mapView.centerCoordinate = userLocation.location.coordinate(如果直接寫在代理方法中,需要在代理方法末尾調(diào)用[_locService stopUserLocationService] 方法,讓定位停止,要不然一直定位,你的地圖就一直鎖定在一個位置)。
當(dāng)然了,想要動畫的話,我們還是這樣設(shè)置:
[_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
然后定位吧,地圖上就顯示出一個藍(lán)色小點(diǎn),然后你高興了吧,沒等你高興多久,產(chǎn)品跑來給你說,定位的圖片我們需要用我們自己設(shè)計的圖片,傻了吧。接下來就需要用到自定義標(biāo)注了。
4.標(biāo)注 講標(biāo)注之前,需要弄懂兩個類的區(qū)別,BMKPointAnnotation和BMKAnnotationView,很多初入地圖行的人都弄不清(我自己一開始也是),前者官方解釋的是一個點(diǎn)的標(biāo)注,后者則是標(biāo)注視圖,好像還是不清楚0.0。。。。按照我的理解就是:前者代表一個點(diǎn),比如你地圖上有很多紅色大頭針,大頭針往那里一插,是不是有個點(diǎn)?這個點(diǎn),就表示BMKPointAnnotation,具有地理的經(jīng)緯度特性(其他的一些信息也可以寫在這里,如果你的后臺將一系列信息包括經(jīng)緯度信息傳給你的話,你就可以重寫這個類)。BMKAnnotationView則代表這個紅色大頭針,什么?紅色大頭針不好看,我要換成德瑪西亞巨劍,好的,直接在這個BMKAnnotationView內(nèi)部添加image即可,怎么樣,弄懂了嗎?? 好,下面來代碼,走起~ 首先解決第一個問題,系統(tǒng)藍(lán)色定位小圓點(diǎn)太丑,要換我們自己的,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];,運(yùn)行的時候他會自動去尋找BMKAnnotationView,這個需要在標(biāo)注代理方法中寫。
(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);
//定義改標(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)氣泡時候觸發(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
自定義的標(biāo)注完成了,自定義氣泡呢?這涉及到一個paopaoView,也就是BMKAnnotationView內(nèi)部的一個屬性,它就是點(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
還有我們什么時候重寫B(tài)MKPointAnnotation呢,一般來說他只是傳個地理位置信息,當(dāng)我們在地圖上想要顯示多個地理位置信息的時候,比如后臺返回來一個數(shù)組,里面每個元素都是一個字典,每個字典代表一個單位,除了地理位置信息,還有其他信息,比如名字,圖片等,對應(yīng)的是每個地理位置,這時候重寫B(tài)MKPointAnnotation,并在其中定義一個model屬性,用來接收后臺返回來的model信息串。然后聲明這個pointAnnotation類的對象,并給他賦值后臺返回來的model信息串中的位置信息,并將整個model傳給他,后面的操作和上面的步驟一樣。 具體想怎么做,看你自己~~~
5.POI檢索 我們先定義一個輸入框,然后根據(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)用一般是移動地圖,然后顯示附近的地理信息(我們常見的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);
//調(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(@"找不到");
}
}
移動地圖,效果如下:
 geo.gif
當(dāng)然了,如果你想配合著poi使用,只需要把result.poiList數(shù)據(jù)源存儲到數(shù)組中,然后在tableView中顯示出來,你會了嗎?~~~~
7.路徑規(guī)劃 將目的地和初始地點(diǎn)連接起來,通過百度內(nèi)部API來告訴我們怎么去(也就是去一個地方查地圖一樣) 首先導(dǎo)入<BaiduMapAPI_Search/BMKRouteSearch.h>
<BaiduMapAPI_Utils/BMKUtilsComponent.h>這兩個頭文件,并遵循代理BMKRouteSearchDelegate。
在.m文件最上面,我們還要聲明一個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
同時,我們定義一個屬性
@property (nonatomic, strong) BMKRouteSearch *routeSearch;
在button點(diǎn)擊方法里實現(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,也就是公交(地鐵)方式,如果你想用別的(比如步行,或者騎乘,駕車等),可在這替換方法
由于上面我們用的是公交,所以下面我們要實現(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ù)累計
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)有歧義時通,獲取建議檢索起終點(diǎn)
//result.routeAddrResult
}
else {
NSLog(@"抱歉,未找到結(jié)果");
}
}
在上面結(jié)尾那里我們添加了路線overlay,所以我們要實現(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ù)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)注的代理方法中實現(xiàn)我們自定義的標(biāo)注
if ([annotation isKindOfClass:[RouteAnnotation class]]) {
return [self getRouteAnnotationView:mapView viewForAnnotation:annotation];
}
這里面調(diào)用了一個方法,我們用百度SDK中給我們寫好的,由于他們寫的較多,還涉及到其他文件,在這里我們就舉個例子,所以有的我給注掉了,咱們就看個顯示效果,好看點(diǎn)的效果到時候咱們再去百度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ī)劃,實現(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àn)別的種類,比如駕車(會有提示到哪拐彎之類的),你可以自己根據(jù)百度SDK中寫的改一改(這個路徑規(guī)劃還是看一看百度SDK吧,我在這不多寫了,畢竟方式都一樣,只是種類多)。
斷斷續(xù)續(xù)寫了好幾天,挺多的,也希望能幫到不怎么會地圖集成的小伙伴們~ 更新:有的小伙伴需要demo,我上傳了github上,地址:https://github.com/Feijunjie/BaiduMapDemo/tree/master 所以,如果你覺得我寫的不錯或者幫到您了,就給我點(diǎn)個贊吧
|