在Google地图v3中更改多重折线笔画的颜色 [英] Changing color for the Multiple Polyline stroke on google map v3 in javascript

查看:138
本文介绍了在Google地图v3中更改多重折线笔画的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 http://www.aspsnippets.com/Articles/Google-Maps-V3-Draw-route-line-between-two-geographic-locations-Coordinates-Latitude- and-Longitude-points.aspx 在这里,我可以有n个点数
我想改变笔画的颜色,它没有反映

 <!DOCTYPE html> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< title>< / title>
< / head>
< body>
< script type =text / javascriptsrc =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);

// ***********路由**************** //

//初始化路径数组
var path = new google.maps.MVCArray();

//初始化定向服务
var service = new google.maps.DirectionsService();

//设置路径描边颜色
var poly = new google.maps.Polyline({map:map,strokeColor:'red'});



//循环和绘制路径MAP
之间的路径(var i = 0; i 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 =dvMapstyle =width:500px; height:500px>
< / div>
< / body>
< / html>

并将此行替换为

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

,我尝试使用下面的代码添加颜色作为

var colorVariable = [white,green,blue,yellow,rose ]。



(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);
}

但我没有得到不同的颜色描边,请说明如何整合它它是错误的

解决方案

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



我添加了第4个点(只看到1个颜色,看它是否有效)。

这是你想要的吗?

 <!DOCTYPE html> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< title>改变Google地图上多重折线笔画的颜色< / title>
< / head>
< body>
< script type =text / javascriptsrc =http://maps.googleapis.com/maps/api/js?sensor=false>< / script>
< script type =text / javascript>
var polylines = []; //让我们为每种颜色制作1条折线

var marker = [
{
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 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);
// ***********路由**************** //
//制作折线
/ / var Loop和Draw Path在MAP
上的点之间的路由(var i = 0; i generatePath(i); //我把这一切都放在一个函数
}

//我们想知道哪个请求正在被处理
函数indexOfRequest(result){
for(var i = 0; i <= lat_lng.length; i ++){
if(result.request.origin == lat_lng [i]){
return i;


$ b函数generatePath(i){
//颜色
var colorVariable = [white,green,blue ,黄色,玫瑰];
//初始化定向服务
var service = new google.maps.DirectionsService();
//初始化路径数组
var path = new google.maps.MVCArray();
var color = colorVariable [i%colorVariable.length]; //%确保数组循环,所以在玫瑰后,它再次变成白色
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);
//路线服务
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 =dvMapstyle =width:500px; height:500px>
< / div>
< / body>
< / html>


I using the same code from http://www.aspsnippets.com/Articles/Google-Maps-V3-Draw-route-line-between-two-geographic-locations-Coordinates-Latitude-and-Longitude-points.aspx here i can have n number of point I am trying to change the color of stroke it is not reflecting

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

and replaced this line with

 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);
        }

but i am not getting the different color strokes please say how to integrated it what is wrong in it

解决方案

I used a trick to find out which request is being processed, by looking at if (result.request.origin == lat_lng[i]) . (There might exist more elegant solutions)

I added a 4th point (just to see 1 more color, see if it works).

Is this what you want?

<!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地图v3中更改多重折线笔画的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆