Javascript for Google Fusion Table图层工作,但试图整理代码 [英] Javascript for Google Fusion Table layers working but trying to tidy code
问题描述
我正在使用Google Fusion Tables创建地图。
I'm creating a map using Google Fusion Tables. It has several layers that are displayed or hidden depending on which checkboxes are ticked.
我创建了一些代码来打开/关闭图层,使用一个变量(layer10on)来显示或隐藏这些图层跟踪每个图层(layer10)当前是否可见:
I created some code to turn the layers on/off, using a variable (layer10on) to keep track of whether each layer (layer10) was currently visible:
function toggleLayer10() {
if(layer10on) { layerl0.setOptions({map: null}); }
if(!layer10on) { layerl0.setOptions({map: map}); }
layer10on=!layer10on;
}
...
<input type="checkbox" checked onchange="toggleLayer10();" />
这样做可以,但我不想为每个复选框/图层复制此代码所以我试图传递的参数,复选框被勾选,和哪个层相关,但它不工作:
This works okay, but I don't want to duplicate this code for each of the checkboxes / layers so I'm trying to pass parameters for which checkbox is ticked, and which layer that relates to, but it's not working:
function toggleLayer(layerToChange,checkboxID) {
if(!document.getElementById(checkboxID).checked) {
layerToChange.setOptions({map: null});
}
if(document.getElementById(checkboxID).checked) {
layerToChange.setOptions({map: map});
}
}
...
<input value="layer10" id="check10" type="checkbox" checked onchange="toggleLayer(this.value,this.id);" />
自从我做了任何编码已经有几年了,所以任何帮助,让我知道我在哪里错了非常感谢。
It's been years since I did any coding so any help to let me know where I'm going wrong would be much appreciated.
感谢,
nicola
推荐答案
我在此答案中展示了一个示例toggleLayer,但它不是那么简洁。 this_layer是实际的FT层,应该是全局的。
I showed an example toggleLayer in this answer but it's not that concise. this_layer is the actual FT Layer which should be a global. This checks whether the layer is visible or not.
function toggleLayer(this_layer)
{
if( this_layer.getMap() ){
this_layer.setMap(null);
}else{
this_layer.setMap(map);
}
}
您需要将地图作为全局以及你的图层。我想,但是没有测试你的复选框应该看起来像:
You need to have both your map as a global as well as your layers. I think, but have not tested that your checkboxes should look like:
<input value="layer10" id="check10" type="checkbox" checked onchange="toggleLayer(layer10);" />
这篇关于Javascript for Google Fusion Table图层工作,但试图整理代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!