简体   繁体   English

如何在单个 Google 地图上制作多个叠加层?

[英]How to make multiple Overlays on a single Google Map?

I'm using an overlay on Google Maps to plot an alternate image on the map, and it's working fine.我正在使用 Google 地图上的叠加层在地图上绘制备用图像,并且工作正常。 I'm using OverlayView() , and I'm able to create some designs on the Overlay too via the drawing API, which is great.我正在使用OverlayView() ,我也可以通过绘图 API 在 Overlay 上创建一些设计,这很棒。

The issue is I want to add an additional overlay in between the two to create some opacity (a slight screening effect) on the underlying map.问题是我想在两者之间添加一个额外的叠加层,以在底层地图上创建一些不透明度(轻微的屏蔽效果)。

Do I use two different overlay methods on the same map?我是否在同一张地图上使用两种不同的叠加方法?

Here's where it's at.这是它的位置。

I have this.maps set to be the GMaps library, and this.map set to be the map instance itself.我将this.maps设置为 GMaps 库,并将this.map设置为地图实例本身。

  createOverlay() {

    MyOverlay.prototype = new this.maps.OverlayView()

    /* Constructor for the OverlayView() class */
    function MyOverlay(bounds, image, map) {
      /* Set up local properties */
      this.bounds_ = bounds
      this.image_ = image
      this.map_ = map

      /* Set up property to hold the overlay, which is added in onAdd() */
      this.div_ = null

      /* Explicitly attach this overlay. */
      this.setMap(map)
    }

    /**
     * onAdd() - called when the map's panes are ready and the overlay is added
     */
    MyOverlay.prototype.onAdd = function () {
      /* Create the element to hold the overlay */
      const evDiv = document.createElement('div')
      evDiv.style.borderStyle = 'none'
      evDiv.style.borderWidth = '0px'
      evDiv.style.position = 'absolute'

      /* Create the image element for the overlay and attach it */
      const evImg = document.createElement('img')
      evImg.src = this.image_
      evImg.style.width = '100%'
      evImg.style.height = '100%'
      evImg.style.position = 'absolute'
      evDiv.appendChild(evImg)

      this.div_ = evDiv

      /* Attach the elements to the map */
      const panes = this.getPanes()
      panes.overlayLayer.appendChild(evDiv)

    }

    /**
     * draw() - called whenever the map's tiles are touched to adjust the overlay
     *        - uses sw and ne coords of the overlay to set its size and peg it to
     *          the correct position and size.
     */
    MyOverlay.prototype.draw = function () {
      /* Retrieve the projection from the overlay */
      const overlayProjection = this.getProjection()

      /* Convert the coords to pixels and resize the div */
      const sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest())
      const ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast())
      const div = this.div_
      div.style.left = sw.x + 'px'
      div.style.top = ne.y + 'px'
      div.style.width = (ne.x - sw.x) + 'px'
      div.style.height = (sw.y - ne.y) + 'px'
    }

    /* onRemove() - Called if we remove the overlay via setting it to 'null' - Not needed yet */
    MyOverlay.prototype.onRemove = function () {
      this.div_.parentNode.removeChild(this.div_)
      this.div_ = null
    }

    const { src, latMax, lngMax, latMin, lngMin } = this.state.Imagery

    const boundaries = new this.maps.LatLngBounds(
      new this.maps.LatLng(latMin, lngMin),
      new this.maps.LatLng(latMax, lngMax),
    )

    this.mapOverlay = new MyOverlay(boundaries, src, this.map)

  }

The overlay is working fine and loading.覆盖层工作正常并且正在加载。 I need to add another overlay in between this overlay and the underlying map.我需要在此叠加层和底层地图之间添加另一个叠加层。

Use panes to layer overlay elements on the map.使用panes在地图上分层叠加元素。

This won't work in every scenario, but if you need some type of extra DOM layer between the map base layer and the overlay of the image, it works well.这并不适用于所有场景,但如果您需要在地图基础层和图像叠加层之间添加某种类型的额外 DOM 层,它会很好地工作。 This solution works perfectly for the situation I was in, where I wanted a screen of opacity on top of the underlying map.这个解决方案非常适合我所处的情况,我希望在底层地图顶部有一个不透明的屏幕。

You don't create two entirely separate OverlayView s over the same map.您不会在同一张地图上创建两个完全独立的OverlayView

Instead, to do this, assuming you've followed the Custom Overlays Documentation to set up your primary overlay, you add code to the configuration of your OverlayView to make another layer.相反,要做到这一点,假设您已经按照自定义叠加文档来设置您的主要叠加,您可以将代码添加到 OverlayView 的配置中以创建另一个图层。

In the constructor, you add a holding div for your additional layer, which we'll call layer :在构造函数中,为附加层添加一个保持 div,我们将其称为layer

     /* Set up properties to hold the overlay, which is added in onAdd() */
      this.layer_ = null
      this.div_ = null

Then when initiating the Overlay with the onAdd() , you set up your additional div.然后在使用onAdd()启动 Overlay 时,设置额外的 div。 The crucial part here is properly using the available panes that google maps exposes as MapPanes for your use:这里的关键部分是正确使用谷歌地图作为MapPanes公开的可用panes供您使用:

    MyOverlay.prototype.onAdd = function () {
      /* Create a layer in between google and overlay tiles */
      const layerDiv = document.createElement('div')
      layerDiv.style.backgroundColor = 'white'
      layerDiv.style.opacity = '0.5'
      layerDiv.style.position = 'absolute'
      /* Any other properties here for your additional layer element */

      this.layer_ = layerDiv

      /* Attach the elements to the proper pane layers of the map */
      const panes = this.getPanes()
      panes.mapPane.appendChild(layerDiv)
      panes.overlayLayer.appendChild(div)

You don't have to touch the draw() function, but in the onRemove() you need to remove your additional overlay pane:您不必触摸draw()函数,但在onRemove()您需要删除额外的覆盖窗格:

    MyOverlay.prototype.onRemove = function () {
      this.layer_.parentNode.removeChild(this.mask_)
      this.layer_ = null
      this.div_.parentNode.removeChild(this.div_)
      this.div_ = null
    }

That's it, you should now have an additional overlay appearing between your primary overlay and the underlying map.就是这样,您现在应该在主叠加层和底层地图之间出现一个额外的叠加层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM