【问题标题】:Google Maps OverlayView: make only SVG clickableGoogle Maps OverlayView:仅使 SVG 可点击
【发布时间】:2018-10-23 22:04:31
【问题描述】:

我有一个带有多个 SVG 叠加层的 Google 地图。但是当我在这些 SVG 上添加点击事件时,所有的叠加层都是可点击的,我希望它只适用于 SVG 路径。

这是一个小提琴 https://jsfiddle.net/gmkfhr9s/1/

我使用 Custom overlays 文档作为基础 https://developers.google.com/maps/documentation/javascript/customoverlays

USGSOverlay.prototype.onAdd = function() {
  var div = document.createElement('div');
  div.style.borderStyle = 'dotted';
  div.style.borderWidth = '2px';
  div.style.borderColor = 'white';
  div.style.position = 'absolute';

  // Create the img element and attach it to the div.
  var img = document.createElement('img');
  img.src = this.image_;
  img.style.width = '100%';
  img.style.height = '100%';
  img.style.position = 'absolute';
  div.appendChild(img);

  this.div_ = div;

  // Add the element to the "overlayLayer" pane.
  var panes = this.getPanes();
  panes.overlayLayer.appendChild(div);

  //add element to clickable layer
  this.getPanes().overlayMouseTarget.appendChild(div);

  google.maps.event.addDomListener(img, 'mouseover', function() {
    img.style.opacity = '0.5';
  });
  google.maps.event.addDomListener(img, 'mouseout', function() {
    img.style.opacity = '1';
  });
};

您可以看到,通过 mouseover 事件,所有覆盖(带边框的框)都被选中,并且只有 SVG。

是否可以只使 SVG 可点击?

如果这对您有帮助的话,这个thread 是一个类似的问题。


编辑:

@Sphinxxx 的回答效果很好。

我只想补充一点,如果您有多个 SVG,其中一些高于其他 SVG,则必须添加此 CSS 才能点击它们。

svg {
  pointer-events: none;
}
path {
  pointer-events: all;
}

【问题讨论】:

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


    【解决方案1】:

    首先,要控制 SVG 的内部 <path>s,您需要 SVG 元素本身,您不能通过具有外部 SVG url 的图像访问它们。

    一旦你有了你的 SVG 元素(最简单的方法是在你的 HTML 中包含 SVG 标记),这篇文章有一个很好的例子来说明如何使用 SVG 作为地图覆盖:http://serversideguy.com/2017/10/31/how-do-i-place-svgs-on-a-google-map-using-custom-overlays/

    我这里做了一个完整的例子:https://codepen.io/Sphinxxxx/pen/wjEyMm

    /**
        Will be called when the map is ready for the overlay to be attached.
    */
    onAdd() {
        const svg = this.svg;
        svg.style.position = 'absolute';
    
        //Add the SVG element to a map pane/layer that is able to receive mouse events:
        const panes = super.getPanes();
        panes.overlayMouseTarget.appendChild(svg);
    }
    
    /**
        Whenever we need to (re)draw the overlay on the map, including when first added.
    */
    draw() {
        //Here, we need to find the correct on-screen position for our image.
        //To achieve that, we simply ask the map's projection to calculate viewport pixels from the image's lat/lng bounds:
        const projection = super.getProjection(),
              bounds = this.bounds,
              sw = projection.fromLatLngToDivPixel(bounds.getSouthWest()),
              ne = projection.fromLatLngToDivPixel(bounds.getNorthEast());
    
        //Place/resize the SVG element:
        const s = this.svg.style;
        s.left = sw.x + 'px';
        s.top  = ne.y + 'px';
        s.width  = (ne.x - sw.x) + 'px';
        s.height = (sw.y - ne.y) + 'px';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 2019-01-31
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多