Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ground Overlay with Transparency

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);
like image 551
user2510431 Avatar asked Dec 16 '13 20:12

user2510431


People also ask

What is a ground overlay?

Ground overlays are image overlays that are tied to latitude/longitude coordinates, so they move when you drag or zoom the map.

How do I change transparency in Google Earth?

On the control panel to the left of the Google Earth image, click the layer you have added, and use the slider to adjust the transparency of the layer.


1 Answers

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>
like image 120
geocodezip Avatar answered Sep 27 '22 17:09

geocodezip