bmk.png
這是我自己分享的第一個項目香嗓,請大家指點指點
在使用百度地圖的時候遇到的一個需求额获,多點路徑規(guī)劃,按照百度地圖官方文檔晶衷,寫的有點麻煩蓝纲,就自己稍微封裝了一下 使用方法
NSArray *keys = @[@"百度大廈",@"天安門",@"長城"];
NSMutableArray *items = [[NSMutableArray alloc]init];
for (int i = 0; i < keys.count; i++) {
YBMKAddressItem *item1 = [[YBMKAddressItem alloc]init];
item1.city = @"北京"; //城市名
item1.key = keys[i]; //關鍵詞
[items addObject:item1];
}
[YBMKMapRoutePlan startBMRoutePlan:items success:^(BMKPolyline *line, NSArray *annotations) {
for (RouteAnnotation* item in annotations) {
[_mapView addAnnotation:item];
}
[_mapView addOverlay:line];
[self mapViewFitPolyLine:line];
}];
新建三個點,按照順序排列晌纫,在block里處理路徑規(guī)劃的結果
使用百度地圖路徑規(guī)劃的時候税迷,遇到兩個問題,記錄一下
1锹漱,沒有路線的問題是 BMKMapViewDelegate 的兩個代理方法沒有實現(xiàn)
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[RouteAnnotation class]]) {
return [(RouteAnnotation*)annotation getRouteAnnotationView:view];
}
return nil;
}
- (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;
}
2箭养,圖標不對,RouteAnnotation.m 中會制定起點哥牍、終點毕泌、途徑點的圖片,按照你自己的需求修改就好
view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"start_node"];
if (view == nil) {
view = [[BMKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"start_node"];
view.image = [UIImage imageNamed:@"icon_nav_start.png"];
view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
}