Ground Overlay with Transparency

8.2k views Asked by At

I have an online map that contains a ground-overlay image, and would like to make the image semi-transparent so that the base map will show through. Is it possible to add a transparency value to this overlay?

http://www.tpwd.state.tx.us/fishboat/fish/recreational/lakes/ingulf1c.phtml

Here's the code for the ground overlay:

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


function initialize() {
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(28.0041,-96.3618),
    zoom: 7,
    mapTypeId: 'roadmap'
  });
  overlay = new google.maps.GroundOverlay(
  '/fishboat/fish/recreational/lakes/images/statemaps/gulfregion.gif',
  imageBounds);
  overlay.setMap(map);
1

There are 1 answers

0
geocodezip On

There is a setOpacity method of the GroundOverlay (works for me with values between 0 and 1.0):

var overlay = null;

function initialize() {
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(28.0041,-96.3618),
    zoom: 7,
    mapTypeId: 'roadmap'
  });
  overlay = new google.maps.GroundOverlay('http://www.tpwd.state.tx.us/fishboat/fish/recreational/lakes/images/statemaps/gulfregion.gif',
      imageBounds);
  overlay.setMap(map);

}

google.maps.event.addDomListener(window, 'load', initialize);

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

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

working example - proof of concept fiddle

screenshot of resulting map

code snippet:

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);
}
html,
body {
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
}

#map {
  width: 100%;
  height: 90%;
}
<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>