透明色彩叠加到Google卫星地图?

时间:2014-01-15 21:27:55

标签: javascript google-maps

我需要将颜色色调应用于Google卫星地图。

我知道可以设置RoadMaps的样式,但API文档说卫星图像无法实现这一点 - 我猜是因为它们是照片。

但是我可以使用平铺透明PNG层来达到预期的效果吗?虽然在色调层上方仍然有可点击的标记吗?

API文档描述了多边形叠加,但示例都附加到了latlng点。我想要覆盖整个画布。

1 个答案:

答案 0 :(得分:4)

文档中有一个相当简单的自定义地图示例:

https://google-developers.appspot.com/maps/documentation/javascript/examples/full/maptype-overlay

将该示例中的getTile例程更改为以下版本会产生绿色的叠加,标记和infowindows仍然按预期工作(未经过特别好的测试):

CoordMapType.prototype.getTile = function(coord, zoom, ownerDocument) {
  var div = ownerDocument.createElement('div');
  div.style.width = this.tileSize.width + 'px';
  div.style.height = this.tileSize.height + 'px';
  div.style.fontSize = '10';
  div.style.backgroundColor = '#00FF00';
  div.style.opacity = 0.4;
  return div;
};

Working example

screenshot of resulting map

代码段

/** @constructor */
function CoordMapType(tileSize) {
  this.tileSize = tileSize;
}

CoordMapType.prototype.getTile = function(coord, zoom, ownerDocument) {
  var div = ownerDocument.createElement('div');
  //  div.innerHTML = coord;
  div.style.width = this.tileSize.width + 'px';
  div.style.height = this.tileSize.height + 'px';
  div.style.fontSize = '10';
  //  div.style.borderStyle = 'solid';
  //  div.style.borderWidth = '1px';
  //  div.style.borderColor = '#AAAAAA';
  div.style.backgroundColor = '#00FF00';
  div.style.opacity = 0.4;
  return div;
};

var map;
var chicago = new google.maps.LatLng(41.850033, -87.6500523);

function initialize() {
  var mapOptions = {
    zoom: 10,
    center: chicago,
    mapTypeId: google.maps.MapTypeId.HYBRID
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);

  var marker = new google.maps.Marker({
    position: chicago,
    title: "test",
    map: map
  });
  var infowindow = new google.maps.InfoWindow({});
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent("Hello<br>" + marker.getPosition().toUrlValue(6));
    infowindow.open(map, marker);
  });

  // Insert this overlay map type as the first overlay map type at
  // position 0. Note that all overlay map types appear on top of
  // their parent base map.
  map.overlayMapTypes.insertAt(
    0, new CoordMapType(new google.maps.Size(256, 256)));
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map-canvas"></div>