我有一个谷歌地图融合表,现在只有2层。我想要做的是在某个缩放级别关闭图层后。我没有在文档中看到可见或可见性,或者我只是在某处查看它。
由于
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(23.448356567844545, -92.32286625000006),
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
layer_0 = new google.maps.FusionTablesLayer({
query: {
select: "col0",
from: "1xXcmTvT8EG9z67lJPTf7-8FIgo9gAPiRbhEk6RqT"
},
map: map,
styleId: 2,
templateId: 2
});
layer_1 = new google.maps.FusionTablesLayer({
query: {
select: "col6>>1",
from: "12jdBXrVylp6U02-VteOcttT3PTM_K0uBVvO2LABy"
},
map: map,
styleId: 2,
templateId: 2
});
google.maps.event.addListener(map, 'zoom_changed', function () {
var zoomLevel = map.getZoom();
if (zoomLevel > 3) {
console.log(zoomLevel);
map.layer_0.visibility = 'hidden';
}
});
答案 0 :(得分:1)
设置"地图"图层的属性为null以将其从地图中删除。你的另一个问题是layer_0不是" map"的一个属性。变量
google.maps.event.addListener(map, 'zoom_changed', function () {
var zoomLevel = map.getZoom();
if (zoomLevel > 3) {
console.log(zoomLevel);
layer_0.setMap(null);
}
});
工作代码段:
function initialize() {
var map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(23.448356567844545, -92.32286625000006),
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var layer_0 = new google.maps.FusionTablesLayer({
query: {
select: "col0",
from: "1xXcmTvT8EG9z67lJPTf7-8FIgo9gAPiRbhEk6RqT"
},
map: map,
styleId: 2,
templateId: 2
});
var layer_1 = new google.maps.FusionTablesLayer({
query: {
select: "col6>>1",
from: "12jdBXrVylp6U02-VteOcttT3PTM_K0uBVvO2LABy"
},
map: map,
styleId: 2,
templateId: 2
});
google.maps.event.addListener(map, 'zoom_changed', function() {
var zoomLevel = map.getZoom();
if (zoomLevel > 3) {
console.log(zoomLevel);
layer_0.setMap(null);
}
});
}
google.maps.event.addDomListener(window,'load',initialize);

html, body, #map-canvas {
height: 100%;
width: 100%;
padding: 0;
}

<script src="https://maps.googleapis.com/maps/api/js?sensor=false&ext=.js"></script>
<div id="map-canvas"></div>
&#13;