【问题标题】:drawing image overlay in tile behaves strange在平铺中绘制图像覆盖行为很奇怪
【发布时间】:2017-02-10 04:52:54
【问题描述】:

我正在尝试为每个地图图块绘制一个位图 (png) 作为谷歌地图中的叠加层。我希望能够将自己的位图绘制为叠加层。 经过一番摆弄,我弄清楚了如何在画布上绘画。 画线工作正常,但画一个加载的图像表现得很奇怪。对缩放和平移的反应尚不清楚。我试图对 onload 函数感到困惑,但它并没有改善......

我的问题: 我做错了什么? 如何在图块上绘制位图叠加?

谢谢。

另见https://jsfiddle.net/rolandinoman/1twa0p74/8/ 平移和放大/缩小以查看效果。

代码示例

/**
 * sometimes the png is drawn, sometimes it is not .
 * don't get it.
 */

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

CoordMapType.prototype.getTile = function(coord, zoom, ownerDocument) {
  var div = ownerDocument.createElement('div');

  var canvas = ownerDocument.createElement('canvas');
   canvas.id     = "canv";
   w = 256;
   h = 256;
   canvas.width = w;
   canvas.height = h;
   ctx = canvas.getContext('2d');
   cs = getComputedStyle(div);
   /// draw some lines on canvas
   ctx.lineWidth = 1;
   ctx.strokeStyle = "#FF0000";
   ctx.strokeRect(6, 6, w-6, h-6);
   // draw an image on the canvas:
   var image = new Image(256,256);
   image.onload = function(){ ctx.drawImage(image, 0, 0);}
   // picked a random bitmap 256X256:
   image.src = "http://files.softicons.com/download/application-icons/malware-icons-by-deleket/png/256x256/Malware.png"

   //ctx.drawImage(image, 0, 0);  // alternative to onload, behaves also erratic.
   div.appendChild(canvas)

   return div;
};

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 10,
    center: {lat: 41.850, lng: -87.650}
  });

  // 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)));
}

【问题讨论】:

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


    【解决方案1】:

    我相信您对 onload 事件有疑问;如果图像已经在缓存中,它不会触发(至少在某些情况下)。

    您可以预加载图像,然后不使用 onload 事件。

    代码 sn-p:

    /** @constructor */
    function CoordMapType(tileSize) {
      this.tileSize = tileSize;
    }
    
    CoordMapType.prototype.getTile = function(coord, zoom, ownerDocument) {
      var div = ownerDocument.createElement('div');
      var canvas = ownerDocument.createElement('canvas');
      w = 256;
      h = 256;
      canvas.width = w;
      canvas.height = h;
      ctx = canvas.getContext('2d');
      cs = getComputedStyle(div);
      /// draw something on canvas
      ctx.lineWidth = 1;
      ctx.strokeStyle = "#FF0000";
      ctx.strokeRect(6, 6, w - 6, h - 6);
      var image = new Image(256, 256);
      image.src = "http://files.softicons.com/download/application-icons/malware-icons-by-deleket/png/256x256/Malware.png"
      ctx.drawImage(image, 0, 0);
      div.appendChild(canvas);
      return div;
    };
    
    function initMap() {
      document.getElementById('nodisp').style.display = "none";
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: {
          lat: 41.850,
          lng: -87.650
        }
      });
    
      // 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', initMap);
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    #map {
      height: 100%;
    }
    <div id="map"></div>
    <!-- Replace the value of the key parameter with your own API key. -->
    <script src="https://maps.googleapis.com/maps/api/js">
    </script>
    <img id="nodisp" src="http://files.softicons.com/download/application-icons/malware-icons-by-deleket/png/256x256/Malware.png" />

    【讨论】:

    • 谢谢@geocodezip。我了解我作为示例制作的这种特殊情况。但是在我的项目中,每个缩放级别/平铺都有不同的图像。我无法全部预加载...如果没有预加载,那么,** 有时**,它不会绘制...您必须进行一些放大和缩小才能实现。当图像不是唯一的时,它会更频繁地发生......参见小提琴链接:link
    猜你喜欢
    • 2010-11-24
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2021-06-09
    相关资源
    最近更新 更多