I've got a simple Google Map with two polygons defined and visible when a page loads.
I've tried adding buttons to turn off a polygon using ".setMap(null)", but it doesn't work for me.
Can you suggest a fix? I feel like I don't quite know enough without help. (you'll need a Google Maps API Key - sorry!).
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Triangles</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=<MAPS KEY GOES HERE>"></script>
<script type="text/javascript">
function initialize() {
// Initialize a new map:
const myLatLng = new google.maps.LatLng(55.73,-6.25); // Center Point of map
const myOptions = { zoom: 10, center: myLatLng, mapTypeId: google.maps.MapTypeId.HYBRID }; // Map options
const map = new google.maps.Map(document.getElementById('map'), myOptions); // create "map"
// Define & draw a yellow triangle polygon:
var yellowCoords = [ { lat: 55.828897, lng: -6.398249 }, { lat: 55.835067, lng: -6.126337 }, { lat: 55.674712, lng: -6.305552 } ];
var yellowPolygon = new google.maps.Polygon({ paths: yellowCoords, strokeColor: 'yellow', strokeOpacity: .7, strokeWeight: 2, fillColor: 'yellow', fillOpacity: 0.2 });
yellowPolygon.setMap(map);
// Define & draw a red triangle polygon:
var redCoords = [ { lat: 55.668897, lng: -6.198249 }, { lat: 55.835067, lng: -6.026337 }, { lat: 55.674712, lng: -6.005552 } ];
var redPolygon = new google.maps.Polygon({ paths: redCoords, strokeColor: 'red', strokeOpacity: .7, strokeWeight: 2, fillColor: 'red', fillOpacity: 0.2 });
redPolygon.setMap(map);
} // end function initialize
function yellowOff() {
yellowPolygon.setMap(null);
}
function redOff() {
redPolygon.setMap(null);
}
</script>
<style> #map { height: 800px; width: 800px; }</style>
</head>
<body onLoad="initialize();">
<div id="map"></div>
<button onClick="javascript:yellowOff();" value="Click">Yellow Off</button>
<button onClick="javascript:redOff();" value="Click">Red Off</button>
</body>
</html>
Thank you!