
bmk.png
這是我自己分享的第一個項(xiàng)目,請大家指點(diǎn)指點(diǎn)
在使用百度地圖的時候遇到的一個需求,多點(diǎn)路徑規(guī)劃,按照百度地圖官方文檔,寫的有點(diǎn)麻煩,就自己稍微封裝了一下 使用方法
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]; //關(guān)鍵詞
[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];
}];
新建三個點(diǎn),按照順序排列,在block里處理路徑規(guī)劃的結(jié)果
使用百度地圖路徑規(guī)劃的時候,遇到兩個問題,記錄一下
1,沒有路線的問題是 BMKMapViewDelegate 的兩個代理方法沒有實(shí)現(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,圖標(biāo)不對,RouteAnnotation.m 中會制定起點(diǎn)、終點(diǎn)、途徑點(diǎn)的圖片,按照你自己的需求修改就好
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));
}