javascript - 在 javascript 中更改 google map v3 上多折线描边的颜色

标签 javascript google-maps colors google-polyline

我使用来自 http://www.aspsnippets.com/Articles/Google-Maps-V3-Draw-route-line-between-two-geographic-locations-Coordinates-Latitude-and-Longitude-points.aspx 的相同代码在这里我可以有 n 个点 我正在尝试更改未反射(reflect)的笔划颜色

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        var markers = [
            {
                "title": 'Alibaug',
                "lat": '18.641400',
                "lng": '72.872200',
                "description": 'xxxx'
            }
        ,
            {
                "title": 'Mumbai',
                "lat": '18.964700',
                "lng": '72.825800',
                "description": 'yyyy'
            }
        ,
            {
                "title": 'Pune',
                "lat": '18.523600',
                "lng": '73.847800',
                "description": 'zzz'
            }
];
        window.onload = function () {
            var mapOptions = {
                center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
                zoom: 10,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
            var infoWindow = new google.maps.InfoWindow();
            var lat_lng = new Array();
            var latlngbounds = new google.maps.LatLngBounds();
            for (i = 0; i < markers.length; i++) {
                var data = markers[i]
                var myLatlng = new google.maps.LatLng(data.lat, data.lng);
                lat_lng.push(myLatlng);
                var marker = new google.maps.Marker({
                    position: myLatlng,
                    map: map,
                    title: data.title
                });
                latlngbounds.extend(marker.position);
                (function (marker, data) {
                    google.maps.event.addListener(marker, "click", function (e) {
                        infoWindow.setContent(data.description);
                        infoWindow.open(map, marker);
                    });
                })(marker, data);
            }
            map.setCenter(latlngbounds.getCenter());
            map.fitBounds(latlngbounds);

            //***********ROUTING****************//

            //Intialize the Path Array
            var path = new google.maps.MVCArray();

            //Intialize the Direction Service
            var service = new google.maps.DirectionsService();

            //Set the Path Stroke Color
            var poly = new google.maps.Polyline({ map: map, strokeColor: 'red' });



            //Loop and Draw Path Route between the Points on MAP
            for (var i = 0; i < lat_lng.length; i++) {
                if ((i + 1) < lat_lng.length) {
                    var src = lat_lng[i];
                    var des = lat_lng[i + 1];
                    path.push(src);
                    poly.setPath(path);
                    service.route({
                        origin: src,
                        destination: des,
                        travelMode: google.maps.DirectionsTravelMode.DRIVING
                    }, function (result, status) {
                        if (status == google.maps.DirectionsStatus.OK) {
                            for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
                                path.push(result.routes[0].overview_path[i]);
                            }
                        }
                    });
                }
            }
        }
    </script>
    <div id="dvMap" style="width: 500px; height: 500px">
    </div>
</body>
</html>

并将这一行替换为

 var poly = new google.maps.Polyline({ map: map, strokeColor: 'red' });

and i tried to add color using the below code as

var colorVariable = ["white","green","blue","yellow","rose"];



     for(var a =0;a<=5;a++){
            var polyOptions = {
                strokeColor: colorVariable[a],
                strokeOpacity: 1.0,
                strokeWeight: 2
            }
            poly = new google.maps.Polyline({ map: map, strokeColor: polyOptions });
            poly.setMap(map);
        }

但我没有得到不同的颜色笔画请说出如何整合它有什么问题

最佳答案

一条折线只能有一种颜色,你需要为每种颜色制作一条单独的折线:

function getDirections(src, des, color, map) {
    //Intialize the Direction Service
    var service = new google.maps.DirectionsService();
    service.route({
        origin: src,
        destination: des,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    }, function (result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            //Intialize the Path Array
            var path = [];
            for (var i = 0; i < result.routes[0].overview_path.length; i++) {
                path.push(result.routes[0].overview_path[i]);
            }
            //Set the Path Stroke Color
            var polyOptions = {
                strokeColor: color,
                strokeOpacity: 1.0,
                strokeWeight: 2,
                path: path,
                map: map
            }
            poly = new google.maps.Polyline(polyOptions);
            poly.setMap(map);

        }
    });
}

proof of concept fiddle

代码片段:

var markers = [{
  "title": 'Alibaug',
  "lat": '18.641400',
  "lng": '72.872200',
  "description": 'xxxx'
}, {
  "title": 'Mumbai',
  "lat": '18.964700',
  "lng": '72.825800',
  "description": 'yyyy'
}, {
  "title": 'Pune',
  "lat": '18.523600',
  "lng": '73.847800',
  "description": 'zzz'
}];
// white is hard to see on the map tiles, removed.
var colorVariable = ["green", "blue", "yellow", "rose"];
window.onload = function() {
  var mapOptions = {
    center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
  var infoWindow = new google.maps.InfoWindow();
  var lat_lng = new Array();
  var latlngbounds = new google.maps.LatLngBounds();
  for (i = 0; i < markers.length; i++) {
    var data = markers[i]
    var myLatlng = new google.maps.LatLng(data.lat, data.lng);
    lat_lng.push(myLatlng);
    var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: data.title
    });
    latlngbounds.extend(marker.position);
    (function(marker, data) {
      google.maps.event.addListener(marker, "click", function(e) {
        infoWindow.setContent(data.description);
        infoWindow.open(map, marker);
      });
    })(marker, data);
  }
  map.setCenter(latlngbounds.getCenter());
  map.fitBounds(latlngbounds);

  //***********ROUTING****************//



  //Set the Path Stroke Color
  var poly = new google.maps.Polyline({
    map: map,
    strokeColor: 'red'
  });

  //Loop and Draw Path Route between the Points on MAP
  for (var i = 0; i < lat_lng.length; i++) {
    if ((i + 1) < lat_lng.length) {
      var src = lat_lng[i];
      var des = lat_lng[i + 1];
      getDirections(src, des, colorVariable[i], map);
    }
  }
}

function getDirections(src, des, color, map) {
  //Intialize the Direction Service
  var service = new google.maps.DirectionsService();
  service.route({
    origin: src,
    destination: des,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  }, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      //Intialize the Path Array
      var path = [];
      for (var i = 0; i < result.routes[0].overview_path.length; i++) {
        path.push(result.routes[0].overview_path[i]);
      }
      //Set the Path Stroke Color
      var polyOptions = {
        strokeColor: color,
        strokeOpacity: 1.0,
        strokeWeight: 2,
        path: path,
        map: map
      }
      poly = new google.maps.Polyline(polyOptions);
      poly.setMap(map);

    }
  });
}
html,
body,
#dvMap {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="dvMap"></div>

关于javascript - 在 javascript 中更改 google map v3 上多折线描边的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33689866/

相关文章:

vb.net - 如何在Silverlight 4.0中将RGB值转换为颜色名称?

javascript - 使用 javascript 回显 css style.overflow 属性

javascript - 将内联 SVG 转换为 png 时出现样式错误

javascript - map 应以一个点为中心,并且最近的标记应可见

javascript - 转换颜色十六进制值以在 Three.js json 文件中使用

java - 具有两种颜色的自定义文本样式

javascript - 如何为 php 的链接创建 cookie 但输出不同

javascript - 我对 Papaparse 的定义变得不确定

javascript - Fusion Tables 层中超过最大 URL 长度的请求

javascript - map : Expected mapDiv of type Element but was passed undefined - google maps