我很难让切换与我的图层一起工作。我知道以前有人问过这个问题,并且我已经阅读了回复。我尝试使用 Kathryn 的 toggle_layer.html 代码,但无法正常工作。
当我添加“function changeMap”代码(下面以粗体显示)和 IF 语句代码时,它会删除我的地图。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<style>
#map-canvas { width:500px; height:400px; }
.layer-wizard-search-label { font-family: sans-serif };
</style>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var map;
var layer_0;
var layer_1;
function initialize() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(-23.002644973802262, 135.7124635),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
layer_0 = new google.maps.FusionTablesLayer({
query: {
select: "col2>>1",
from: "1HpTDVhKXNMEs59ins5DfwbaEOpRevJ79yEuKSYw"
},
map: map,
styleId: 2,
templateId: 2
});
layer_1 = new google.maps.FusionTablesLayer({
query: {
select: "col2>>1",
from: "1HpTDVhKXNMEs59ins5DfwbaEOpRevJ79yEuKSYw"
},
map: map,
styleId: 3,
templateId: 4
});
}
google.maps.event.addDomListener(window, 'load', initialize);
function changeMap(layerNum) {
if (layerNum == 2) {
update(layer_0);
}
if (layerNum == 3) {
update(layer_1);
}
}
function update(layer) {
var layerMap = layer.getMap();
if (layerMap) {
layer.setMap(null);
} else {
layer.setMap(map);
}
}
</script>
</head>
<body onload="initialize();">
<input type="checkbox" value="2" onclick="changeMap(this.value)" checked="checked" />Layer 1
<input type="checkbox" value="3" onclick="changeMap(this.value)" checked="checked" />Layer 2
<div id="map_canvas"></div>
</body>
</html>