具有透明度的地面覆盖层

时间:2013-12-16 20:17:18

标签: google-maps-api-3 transparency overlays

我有一个在线地图,其中包含地面叠加图像,并希望使图像半透明,以便基本地图显示出来。是否可以为此叠加层添加透明度值?

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

以下是地面叠加层的代码:

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 个答案:

答案 0 :(得分:11)

有一个setOpacity method of the GroundOverlay(适用于我的值介于0和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

代码段

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

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