我很难让切换到我的图层。我知道之前已经问过这个问题而且我已经阅读了回复。我尝试使用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>
答案 0 :(得分:0)
你的css和html不一致。如果我改变:
<div id="map_canvas"></div>
要:
<div id="map-canvas"></div>
显示地图并切换工作。 working example
(或者,您可以修复css或将css内联)