折线
Polyline表示地图上的折线覆盖物。它包含一组点,并将这些点连接起来形成折线。
添加折线
折线在地图上绘制为一系列直线段。可以自定义这些线段的颜色、粗细和透明度。颜色可以是十六进制数字形式(比如:#ff0000)或者是颜色关键字(比如:red)。
Polyline的绘制需要浏览器支持矢量绘制功能。在Internet Explorer中,地图使用VML绘制折线;在其他浏览器中使用SVG或者Canvas
以下代码段会在两点之间创建6像素宽的蓝色折线:
var polyline = new BMap.Polyline([ new BMap.Point(116.399, 39.910), new BMap.Point(116.405, 39.920) ],{strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5}); map.addOverlay(polyline);
添加折线完整HTML+JS代码示例:
<!DOCTYPE html> <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <title>百度离线地图</title> <style type="text/css"> body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";} #allmap{width:100%;height:100%;} </style> <!-- 引入核心js文件 --> <script type="text/javascript" src="js/apiv.2.0.js"></script> </head> <body> <div id="allmap"></div> </body> </html> <script type="text/javascript"> // 创建Map实例 var map = new BMap.Map("allmap", {enableMapClick:false}); // 设置地图背景色为白色 map.getContainer().style.background = '#FFF'; var point = new BMap.Point(104.074362,30.540276); map.centerAndZoom(point, 5); //创建折线,可设置线条颜色、粗细、透明度 var polyline = new BMap.Polyline([ new BMap.Point(116.399, 39.910), new BMap.Point(110.405, 30.920), new BMap.Point(100.425, 39.900) ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); map.addOverlay(polyline); //增加折线 //清除折线方法 function remove_overlay(){ map.clearOverlays(polyline); } </script>