颜色某些地图图块

时间:2016-12-19 00:19:36

标签: javascript google-maps google-maps-api-3

我正在尝试在地图上设置特定图块的样式但不确定如何。 谷歌有一个我在这里指的瓷砖的例子: https://developers.google.com/maps/documentation/javascript/examples/maptype-overlay

我已经按照我想要的样式设置了瓷砖,但是现在如果我想为特定的瓷砖着色,我该怎么做?

我有一个JSFiddle我的地图示例:

jsFiddle

   function CoordMapType(tileSize) {
    this.tileSize = tileSize;
  }

  CoordMapType.prototype.getTile = function(coord, zoom, ownerDocument) {
    var div = ownerDocument.createElement('div');
    div.innerHTML = "coords:" + coord + ", zoom: " + zoom;
    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 = '#0000ff';
    div.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
    return div;
  };

function initialize() {
 var map = new google.maps.Map(document.getElementById('map'), {
   zoom: 11,
   center: new google.maps.LatLng(47.53772, -122.1153),
   mapTypeId: google.maps.MapTypeId.ROADMAP
 });

 map.overlayMapTypes.insertAt(
        0, new CoordMapType(new google.maps.Size(256, 256)));
var marker = new google.maps.Marker({
    position: montpellier,
    map: map
});
}

initialize();

1 个答案:

答案 0 :(得分:1)

要为特定图块着色,您需要编写代码以检测要设置样式的图块,然后设置其样式。

if (coord.x == 329 && coord.y == 715 && zoom == 11) {
  div.style.backgroundColor = 'rgba(255, 0, 0, 0.8)';  
} else if (coord.x == 329 && coord.y == 716 && zoom == 11) {
   div.style.backgroundColor = 'rgba(0, 255, 0, 0.8)';  
} else if (coord.x == 330 && coord.y == 715 && zoom == 11) {
   div.style.backgroundColor = 'rgba(0, 0, 255, 0.8)';  
} else {
  div.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
}

proof of concept fiddle

style map tiles

代码段



function CoordMapType(tileSize) {
     this.tileSize = tileSize;
   }

   CoordMapType.prototype.getTile = function(coord, zoom, ownerDocument) {
     var div = ownerDocument.createElement('div');
     div.innerHTML = "coords:" + coord + ", zoom: " + zoom;
     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 = '#0000ff';
     if (coord.x == 329 && coord.y == 715 && zoom == 11) {
       div.style.backgroundColor = 'rgba(255, 0, 0, 0.8)';
     } else if (coord.x == 329 && coord.y == 716 && zoom == 11) {
       div.style.backgroundColor = 'rgba(0, 255, 0, 0.8)';
     } else if (coord.x == 330 && coord.y == 715 && zoom == 11) {
       div.style.backgroundColor = 'rgba(0, 0, 255, 0.8)';
     } else {
       div.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
     }
     return div;
   };

   function initialize() {
     var map = new google.maps.Map(document.getElementById('map'), {
       zoom: 11,
       center: new google.maps.LatLng(47.53772, -122.1153),
       mapTypeId: google.maps.MapTypeId.ROADMAP
     });


     map.overlayMapTypes.insertAt(
       0, new CoordMapType(new google.maps.Size(256, 256)));
     var marker = new google.maps.Marker({
       position: {
         lat: 47.5377,
         lng: -122.115
       },
       map: map
     });
   }

   initialize();

#map {
  height: 400px;
  width: 100%;
  border: 6px solid #dedede;
  margin: 0 auto;
}

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  <meta charset="utf-8">
  <title>Google Maps JavaScript API v3 Example: Rectangle Simple</title>
  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
</head>

<body>
  <div id="map"></div>
</body>

</html>
&#13;
&#13;
&#13;