简体   繁体   English

如何在Google Map API中设置图层的不透明度

[英]How to set opacity of layers in Google Map API

I need to set the opacity/alpha a layer in the Google Map API for JavaScript, but can find a way to do it. 我需要在JavaScript的Google Map API中将不透明度/ alpha设置为一层,但是可以找到一种方法来实现。 For both the KmlLayer() and GroundOverlay() types. 对于KmlLayer()和GroundOverlay()类型。

Any ideas? 有任何想法吗?

If you're using a KML/KMZ file, you can adjust the opacity using the XML information. 如果您使用的是KML / KMZ文件,则可以使用XML信息来调整不透明度。

Load your KML/KMZ file into Google Earth, and you'll be able to set the properties on a polygon to have a specific color and opacity. 将您的KML / KMZ文件加载到Google Earth中,就可以将多边形上的属性设置为具有特定的颜色和不透明度。

Alternatively, you can edit the XML directly. 或者,您可以直接编辑XML。 Take a look at http://code.google.com/apis/kml/documentation/kmlreference.html#colorstyle for more information. 有关更多信息,请访问http://code.google.com/apis/kml/documentation/kmlreference.html#colorstyle

Would you consider using a custom overlay instead of a GroundOverlay? 您是否考虑使用自定义叠加层而不是GroundOverlay? For example, take a look at Google's custom overlay tutorial here in which they show how to build a simple overlay that's similar in function to a GroundOverlay. 例如,在这里查看Google的自定义覆盖图教程,其中他们展示了如何构建功能与GroundOverlay相似的简单覆盖图。 In the USGSOverlay.onAdd() method defined in this section, note that you have direct access to the <img> that will be displayed in the overlay and the <div> that contains it. 请注意,在节中定义的USGSOverlay.onAdd()方法中,您可以直接访问将在叠加层中显示的<img>和包含该叠加层的<div>。 For example, to set the opacity to a fixed value like 0.5, you could modify the onAdd() method: 例如,要将不透明度设置为固定值(例如0.5),可以修改onAdd()方法:

/* Set the overlay's div_ property to this DIV */
this.div_ = div;
this.div_.style.opacity = 0.5;  /* ADDED */

Since you want to set the opacity interactively, you'd probably want to add a setOpacity() method to your own version of the USGSOverlay class, and hook the method to a suitable control. 由于您希望以交互方式设置不透明度,因此您可能希望将setOpacity()方法添加到您自己的USGSOverlay类的版本中,并将该方法挂接到合适的控件上。

A completely different approach would be to find the <img> in the DOM. 完全不同的方法是在DOM中找到<img>。 For example, the following code which uses jQuery works: 例如,以下使用jQuery的代码有效:

$("img[src='foo.png']").css('opacity',0.5);

You need to be certain that the image is fully loaded and added to the DOM. 您需要确定图像已完全加载并添加到DOM中。 I haven't figured out how to do this reliably. 我还没有弄清楚如何可靠地做到这一点。

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

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