2013-11-27 61 views
0

我试图让复选框将多边形设置为在地图上可见/不可见。根据下面的代码,它不起作用。我认为,因为我传递的变量类型不正确/需要投射。但我不确定...将单个多边形设置为可见/不可见

如何编写此代码,以便与复选框(同名)关联的区域/多边形的可见性切换?

说实话我是新来的Javascript,我敢肯定我错过了一些简单的东西,但任何帮助将不胜感激!

<script> 
var cheyChumneahCoords = [ 
new google.maps.LatLng(11.567148,104.931901), 
new google.maps.LatLng(11.564994,104.925757), 
new google.maps.LatLng(11.559585,104.927309), 
new google.maps.LatLng(11.562065,104.933274), 
new google.maps.LatLng(11.562276,104.935892), 
new google.maps.LatLng(11.562234,104.935935), 
new google.maps.LatLng(11.562108,104.935977) 
]; 

// Chey Chumneah area 
var cheyChumneahArea=new google.maps.Polygon({ 
    path:cheyChumneahCoords, 
    strokeColor:"#0000FF", 
    strokeOpacity:0.8, 
    strokeWeight:0, 
    fillColor:"#0000FF", 
    fillOpacity:0.4 
    }); 

function areaChange(areaName, checked) 
{ 
alert("Area " + areaName + " changed and is checked: " + checked); //Debug 
cheyChumneahArea.setVisible(checked); //Works 
areaName.setVisible(checked); //Does not work 
} 

function initialize() 
{ 
var mapProp = { 
    center:new google.maps.LatLng(11.562276,104.919434), 
    zoom:14, 
    mapTypeId:google.maps.MapTypeId.ROADMAP 
    }; 
var map=new google.maps.Map(document.getElementById("googleMap") 
    ,mapProp); 

cheyChumneahArea.setMap(map); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 
</script> 
</head> 

<body> 
<div id="googleMap"></div> 

<div id="areaSelection"> 
<input type="checkbox" name="areas" value=cheyChumneahArea onchange="areaChange(this.value, this.checked)">Chey Chumneah<br> 
<input type="checkbox" name="areas" value="BKK1Area" onchange="areaChange(this.value, this.checked)">BKK1 
</div> 

</body> 
</html> 

回答

1

areaName是一个字符串,而不是一个对象。

使用下标表示法:

window[areaName].setVisible(checked); 
+0

完美 - 非常感谢! – Tom