MKMapKit

百度地图多显色线段绘制

2020-01-09  本文已影响0人  zxh123456

百度添加折线绘制

// 添加折线覆盖物
    CLLocationCoordinate2D coords[5] = {0};
    coords[0] = CLLocationCoordinate2DMake(39.968, 116.260);
    coords[1] = CLLocationCoordinate2DMake(39.912, 116.324);
    coords[2] = CLLocationCoordinate2DMake(39.968, 116.373);
    coords[3] = CLLocationCoordinate2DMake(39.912, 116.439);
    coords[4] = CLLocationCoordinate2DMake(39.968, 116.490);
    BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coords count:5];
    [_mapView addOverlay:polyline];

然后代理中设置线段颜色、宽度等属性

- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
    if ([overlay isKindOfClass:[BMKPolygon class]]){
        BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay];
        polygonView.strokeColor = [[UIColor alloc] initWithRed:0.0 green:0 blue:0.5 alpha:1];
        polygonView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:0.2];
        polygonView.lineWidth = 2.0;
        polygonView.lineDashType = kBMKLineDashTypeDot;
        return polygonView;
    }
     if ([overlay isKindOfClass:[BMKPolyline class]]){
             BMKPolylineView *polylineView = [[BMKPolylineView alloc] initWithPolyline:overlay];
             //设置polylineView的画笔颜色为蓝色
             polylineView.strokeColor = [[UIColor alloc] initWithRed:19/255.0 green:107/255.0 blue:251/255.0 alpha:1.0];
             polylineView.lineWidth = 1;
             return polylineView;
        }
    return nil;
}

实际开发中需要同时绘制多个颜色线段,BMKPolylineView没有颜色属性

方法一:自定义继承的子类无法实现,因为BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coords count:5];
方法返回就是BMKPolyline类,而不是子类。

方法二:runtime
创建一个BMKPolyline的Color分类,添加属性lineColor

//.h
@interface BMKPolyline (Color)
@property (nonatomic , copy)  NSString *lineColor;
@end
//.m
#import "BMKPolyline+Color.h"
#import <objc/runtime.h>

static void *strKey = &strKey;

@implementation BMKPolyline (Color)

-(NSString *)lineColor{
    return objc_getAssociatedObject(self, &strKey);
}

-(void)setLineColor:(NSString *)lineColor{
    objc_setAssociatedObject(self, &strKey, lineColor, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

@end

使用的关键代码如下

//绘制线
 BMKPolyline *polyLine = [BMKPolyline polylineWithCoordinates:coors count:2];
 [polyLine setLineColor: model.lineColor];
[_mapView addOverlay:polyLine];

- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{

     if ([overlay isKindOfClass:[BMKPolyline class]]){
         BMKPolylineView *polylineView = [[BMKPolylineView alloc] initWithPolyline:(BMKPolyline *)overlay];
         //设置polylineView的画笔颜色
         NSString *lineColor = [((BMKPolyline *)overlay) lineColor];
         if (lineColor.length<1) {
             lineColor = @"#4169E1";
         }
         polylineView.strokeColor = [ZXHTool colorFromHexRGB:lineColor alpha:1];
        //设置polylineView的画笔宽度
         polylineView.lineWidth = 3;
         return polylineView;
        }
    return nil;
}

IMG_1986.PNG

搞定~

上一篇下一篇

猜你喜欢

热点阅读