Polyline表示地图上的折线覆盖物,它包含一组点,并将这些点连接起来形成折线,你可以通过 BMap.Polyline在地图上添加折线。
首先参考展示地图创建基本地图,之后添加折线覆盖物到地图中。折线覆盖物可以分别设置描边粗细、颜色、填充颜色等属性。
var path = [new BMap.Point(116.399, 39.910),new BMap.Point(116.405, 39.920),new BMap.Point(116.425, 39.900)];var polyline = new BMap.Polyline(path, {strokeColor: "blue",strokeWeight: 2,strokeOpacity: 0.5});map.addOverlay(polyline);
创建 Polyline 时,可以通过第二个参数设置折线的属性,常用属性如下:
strokeColor:设置折线颜色,如 "blue"、"#ff0000" 等。
strokeWeight:设置折线宽度,数值类型,默认值为 2。
strokeOpacity:设置折线透明度,取值范围 0.0 ~ 1.0。
strokeStyle:设置折线样式,可选 "solid"(实线)或 "dashed"(虚线)。
enableEditing:是否启用折线编辑功能,默认 false。编辑效果见 编辑折线示例
var polyline = new BMap.Polyline(path, {strokeColor: "blue",strokeWeight: 4,strokeOpacity: 0.8,strokeStyle: "solid",enableEditing: false});
折线添加到地图后,可以通过实例方法动态更新属性,常用方法如下:
// 修改折线路径var newPath = [new BMap.Point(116.399, 39.910),new BMap.Point(116.415, 39.930)];polyline.setPath(newPath);// 修改折线样式polyline.setStrokeColor("red");polyline.setStrokeWeight(6);polyline.setStrokeOpacity(0.5);polyline.setStrokeStyle("dashed");// 获取折线路径var path = polyline.getPath();
可以通过 strokeTexture设置折线纹理,纹理图的width,height 必须是2的n次方
// 绘制线var polyline = new BMap.Polyline(path, {strokeTexture: {// width,height 需要是2的n次方url: 'https://mapopen-pub-jsapigl.bj.bcebos.com/svgmodel/Icon_road_blue_arrow.png',width: 16,height: 64},strokeWeight: 8});map.addOverlay(polyline);
上一篇
下一篇