11

我可以在谷歌地图中绘制多条折线并设置它们的样式,但我想用不同的颜色为每条折线着色。

目前,我有这个代码:

var DrivePath = [
  new google.maps.LatLng(37.772323, -122.214897),
  new google.maps.LatLng(21.291982, -157.821856),
  new google.maps.LatLng(-18.142599, 178.431),
  new google.maps.LatLng(-27.46758, 153.027892),
  new google.maps.LatLng(12.97918167,   77.6449),
  new google.maps.LatLng(12.97918667,   77.64487167),
  new google.maps.LatLng(12.979185, 77.64479167),
  new google.maps.LatLng(12.97918333, 77.64476)
];


var PathStyle = new google.maps.Polyline({
  path: DrivePath,
  strokeColor: "#FF0000",
  strokeOpacity: 1.0,
  strokeWeight: 2
});

PathStyle.setMap(map);

有什么方法可以为我正在创建的每条折线添加单独的样式?

4

3 回答 3

19

当然。例如,假设您知道每行要使用什么颜色,假设您因此有一个长度等于 DrivePath.length - 1 的颜色数组。

var Colors = [
    "#FF0000", 
    "#00FF00", 
    "#0000FF", 
    "#FFFFFF", 
    "#000000", 
    "#FFFF00", 
    "#00FFFF", 
    "#FF00FF"
];

现在,不是绘制一条折线,而是为每个坐标绘制一条单独的折线。

for (var i = 0; i < DrivePath.length-1; i++) {
  var PathStyle = new google.maps.Polyline({
    path: [DrivePath[i], DrivePath[i+1]],
    strokeColor: Colors[i],
    strokeOpacity: 1.0,
    strokeWeight: 2,
    map: map
  });
}
于 2013-01-17T13:58:26.987 回答
6

用于绘制 2 条不同的折线

    function initialize()
    {

                map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 7,
                    center: {lat: 41.879, lng: -87.624}  // Center the map on Chicago, USA.
                  });

                var polyOptions = {
                    strokeColor: '#000000',
                    strokeOpacity: 1.0,
                    strokeWeight: 3
                };
                var polyOptions2 = {
                    strokeColor: '#FFFFFF',
                    strokeOpacity: 1.0,
                    strokeWeight: 3
                };

                var polyline = new google.maps.Polyline(polyOptions);
                var polyline2= new google.maps.Polyline(polyOptions2);
                polyline.setMap(map);
                polyline2.setMap(map);
                google.maps.event.addListener(map, 'click', addLatLng);
    }
于 2016-06-17T09:11:55.173 回答
0

上面的答案是正确的。这将导致分离的折线。这可以通过添加每条折线的圆形起始端和圆形结束端来改善。

polyline.setEndCap(new RoundCap());
polyline2.setStartCap(new RoundCap());
于 2020-03-27T21:58:20.543 回答