Ground Overlays

by Lawrence Ross

HTML

<div id="map"></div>
<input type="text" id="opacity" value="0.2" />
<input type="button" value="setOpacity" onclick="setOpacity();" />

<!-- 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>

CSS

html,
body {
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
}

#map {
  width: 100%;
  height: 90%;
}

JavaScript

var overlay = null;

function initMap() {
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(28.0041, -96.3618),
    zoom: 7,
    mapTypeId: 'roadmap'
  });

  var imageBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(25.71438633861514, -98.33555959121725),
    new google.maps.LatLng(30.40813339247205, -93.57953893270167));

  overlay = new google.maps.GroundOverlay(
    'http://www.tpwd.state.tx.us/fishboat/fish/recreational/lakes/images/statemaps/gulfregion.gif',
    imageBounds);
  overlay.setMap(map);
  map.fitBounds(imageBounds);
}

function setOpacity() {
  var opacityStr = document.getElementById('opacity').value;
  var opacity = parseFloat(opacityStr);
  overlay.setOpacity(opacity);
}