Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中更改google map v3上多段线笔划的颜色_Javascript_Google Maps_Colors_Google Polyline - Fatal编程技术网

在javascript中更改google map v3上多段线笔划的颜色

在javascript中更改google map v3上多段线笔划的颜色,javascript,google-maps,colors,google-polyline,Javascript,Google Maps,Colors,Google Polyline,我使用相同的代码,从这里我可以得到n个点 我试图改变笔划的颜色,但它没有反映 <!DOCTYPE html > <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <script type="text/javascript" src="http://maps.googleapis.

我使用相同的代码,从这里我可以得到n个点 我试图改变笔划的颜色,但它没有反映

<!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>

变量标记=[
{
“头衔”:“阿里堡”,
“lat”:“18.641400”,
“液化天然气”:“72.872200”,
“说明”:“xxxx”
}
,
{
“标题”:“孟买”,
“lat”:“18.964700”,
“液化天然气”:“72.825800”,
“说明”:“yyyy”
}
,
{
“标题”:“浦那”,
“lat”:“18.523600”,
“液化天然气”:“73.847800”,
“说明”:“zzz”
}
];
window.onload=函数(){
变量映射选项={
中心:新建google.maps.LatLng(标记[0].lat,标记[0].lng),
缩放: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=新阵列();
var latlngbounds=new google.maps.latlngbounds();
对于(i=0;i
并将该行替换为

 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);
        }
var poly=new google.maps.Polyline({map:map,strokeColor:'red'});
我试着用下面的代码添加颜色
var colorVariable=[“白色”、“绿色”、“蓝色”、“黄色”、“玫瑰色”];

对于(var a=0;a,只能使用setOption更改笔划颜色,方法如下:

  poly.setOptions({strokeColor: 'blue'}); 
您的polyOption不是strokeColor属性的合法值。

您可以使用
obj.setOptions({attribute:value})
pattern

在代码中指定笔划颜色,您只能使用setOption更改笔划颜色,方法如下:

  poly.setOptions({strokeColor: 'blue'}); 
您的polyOption不是strokeColor属性的合法值。

您可以使用
obj.setOptions({attribute:value})
pattern(

我使用了一个技巧,通过查看if(result.request.origin==lat_lng[I])(可能存在更优雅的解决方案)来确定正在处理哪个请求

我添加了第四点(只是为了再看一种颜色,看看它是否有效)

这是你想要的吗

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Changing color for the Multiple Polyline stroke on Google maps</title>
</head>
<body>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        var polylines = [];  // let's make 1 polyline per color

        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'
            }, 
            {
                "title": 'Kolhapur',
                "lat": 16.696530010128207,
                "lng": 74.23864138126372,
                "description": 'Extra point'
            }
        ];
        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****************//
            // Make the polyline
            //Loop and Draw Path Route between the Points on MAP
            for (var i = 0; i< lat_lng.length; i++) {
              generatePath(i);  // I put this all in a function
            }

            // we want to know which request is being processed
            function indexOfRequest(result) {
              for (var i = 0; i <= lat_lng.length; i++) {
                if (result.request.origin == lat_lng[i]) {
                  return i;
                }
              }
            }
            function generatePath(i) {
                // colors
                var colorVariable = ["white", "green", "blue", "yellow", "rose"];
                //Intialize the Direction Service
                var service = new google.maps.DirectionsService();
                //Intialize the Path Array
                var path = new google.maps.MVCArray();
                var color = colorVariable[i % colorVariable.length];  // the % makes sure the array cycles, so after rose, it's white again
                var poly = new google.maps.Polyline({ map: map, strokeColor: color });
                polylines.push(poly);
                var src = lat_lng[i];
                var des = lat_lng[i + 1];
                path.push(src);
                poly.setPath(path);
                // route service
                service.route({
                    origin: src,
                    destination: des,
                    travelMode: google.maps.DirectionsTravelMode.DRIVING
                }, function (result, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        var routeIndex = indexOfRequest(result);
                        polylines[routeIndex].setPath(result.routes[0].overview_path);
                    }
                });

            }
        }
    </script>
    <div id="dvMap" style="width: 500px; height: 500px">
    </div>
</body>
</html>

在Google地图上更改多段线笔划的颜色
var polylines=[];//让我们为每种颜色创建一条多段线
变量标记=[
{
“头衔”:“阿里堡”,
“lat”:18.641400,
“液化天然气”:72.872200,
“说明”:“xxxx”
},
{
“标题”:“孟买”,
“lat”:18.964700,
“液化天然气”:72.825800,
“说明”:“yyyy”
},
{
“标题”:“浦那”,
“lat”:18.523600,
“液化天然气”:73.847800,
“说明”:“zzz”
}, 
{
“标题”:“Kolhapur”,
“lat”:16.696530010128207,
“液化天然气”:74.23864138126372,
“说明”:“加分”
}
];
window.onload=函数(){
变量映射选项={
中心:新建google.maps.LatLng(标记[0].lat,标记[0].lng),
缩放: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=[];//新数组();
var latlngbounds=new google.maps.latlngbounds();
对于(i=0;i