如何将Polyline的颜色更改回上一个单击Google Api V2 C#Web App?

时间:2011-08-26 03:20:44

标签: c# asp.net google-maps google-maps-api-2 polyline

我可以轻松更改下面单击按钮的折线颜色,

 var points = [  
     new GLatLng(24.85229, 67.01703),  
     new GLatLng(24.914463, 67.0965958)];
 var polyline = new GPolyline(points, '#ff0000', 5, 0.7); 

 GEvent.addListener(polyline, "click", function() {polyline.setStrokeStyle({ color: "#0000FF" }); });
map.addOverlay(polyline); 

但是我希望在点击时将其颜色更改回前一个颜色,如果它的初始颜色是红色并且点击它会变为蓝色然后在其他点击时它再次变为红色....那我该怎么办?

希望你的回复..

1 个答案:

答案 0 :(得分:2)

有很多解决方案。一个是创建交换函数:

 var activeColor = '#ff0000';
 var inactiveColor = '#0000FF';

 var points = [   
                                   new GLatLng(24.85229, 67.01703),   
                                   new GLatLng(24.914463, 67.0965958)]; 
                                   var polyline = new GPolyline(points, activeColor, 5, 0.7);  

GEvent.addListener(polyline, "click", function() {
           // swap activecolor with inactivecolor
           var color = inactiveColor;
           inactiveColor = activeColor;
           activeColor = color;

           // set activecolor
           polyline.setStrokeStyle({ color: activeColor }); 
       }); 
       map.addOverlay(polyline);