Для использования в качестве функции щелчка HTML подпрограмма должна находиться в глобальной области видимости. В настоящее время она является локальной для вашей initMap
функции.
Вам также понадобится массив layers
в глобальной области видимости.
подтверждение концепции скрипта
![screenshot of map](https://i.stack.imgur.com/AKyG2.png)
фрагмент кода:
var map = null;
var layers = [];
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {
lat: 35.391,
lng: -74.447
},
mapTypeId: google.maps.MapTypeId.SATELLITE,
disableDefaultUI: true,
});
var imageBounds = {
north: 41.68,
south: 30.43,
east: -69.26,
west: -81.91
};
layers[0] = new google.maps.GroundOverlay("https://www.saltwx.com/images/A2020022172000.L2_LAC.S3472.nc.MidAtlantic.chlor_a.png", imageBounds); {
preserveViewport: true
};
}
function toggleLayers(i) {
if (layers[i].getMap() == null) {
layers[i].setMap(map);
} else {
layers[i].setMap(null);
}
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#floating-panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto', 'sans-serif';
line-height: 30px;
padding-left: 10px;
}
<div id="floating-panel">
<input type="checkbox" value="Toggle NOAA Buoy Layer" onclick="toggleLayers(0);" />NOAA Buoys TURN ON
</div>
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>
Вы можете оставить его внутри функции initMap
, если применить ее к элементам HTML с помощью функции google.maps.event.addDomListener
.:
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {
lat: 35.391,
lng: -74.447
},
mapTypeId: google.maps.MapTypeId.SATELLITE,
disableDefaultUI: true,
});
var imageBounds = {
north: 41.68,
south: 30.43,
east: -69.26,
west: -81.91
};
layers[0] = new google.maps.GroundOverlay("https://www.saltwx.com/images/A2020022172000.L2_LAC.S3472.nc.MidAtlantic.chlor_a.png", imageBounds); {
preserveViewport: true
};
function toggleLayers(i) {
if (layers[i].getMap() == null) {
layers[i].setMap(map);
} else {
layers[i].setMap(null);
}
}
google.maps.event.addDomListener(document.getElementById('toggleBuoyLayer'), 'click', function() {
toggleLayers(0);
})
}
подтверждение концепции скрипта
фрагмент кода:
var map = null;
var layers = [];
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {
lat: 35.391,
lng: -74.447
},
mapTypeId: google.maps.MapTypeId.SATELLITE,
disableDefaultUI: true,
});
var imageBounds = {
north: 41.68,
south: 30.43,
east: -69.26,
west: -81.91
};
layers[0] = new google.maps.GroundOverlay("https://www.saltwx.com/images/A2020022172000.L2_LAC.S3472.nc.MidAtlantic.chlor_a.png", imageBounds); {
preserveViewport: true
};
function toggleLayers(i) {
if (layers[i].getMap() == null) {
layers[i].setMap(map);
} else {
layers[i].setMap(null);
}
}
google.maps.event.addDomListener(document.getElementById('toggleBuoyLayer'), 'click', function() {
toggleLayers(0);
})
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#floating-panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto', 'sans-serif';
line-height: 30px;
padding-left: 10px;
}
<div id="floating-panel">
<input type="checkbox" id="toggleBuoyLayer" value="Toggle NOAA Buoy Layer" />NOAA Buoys TURN ON
</div>
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>