包含用于标记的图像的 DOMNode 无法通过 API 获得。
此外,默认情况下,标记不会是单个 DOMNode,它们将通过画布绘制。
但是,当您为每个标记使用唯一的图标 URL 时,可以通过 CSS 访问标记图像。
示例(使用 jQuery):
<style type="text/css">
img[src^='http://www.google.com/mapfiles/marker.png?i=']{
opacity: 0.5
}
</style>
<script type="text/javascript">
function initialize() {
var index=0;
var mapOptions = {
zoom: 14,
center: new google.maps.LatLng(52.5498783, 13.425209099999961),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
marker=new google.maps.Marker({position:map.getCenter(),
map:map,optimized:false,
icon:'http://www.google.com/mapfiles/marker.png?i='+(index++)});
google.maps.event.addListener(marker,'mouseover',function(){
$('img[src="'+this.icon+'"]').stop().animate({opacity:1});
});
google.maps.event.addListener(marker,'mouseout',function(){
$('img[src="'+this.icon+'"]').stop().animate({opacity:.5});
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
如何运作:
示例使用单个图像作为 Marker-Icon (http://www.google.com/mapfiles/marker.png)
我们通过 CSS 应用不透明度。您可能会注意到 URL 中有一个 i 参数。此参数将用于使 img-src 唯一。
我使用一个变量,该变量将被递增以获得唯一的 img-src:
var index=0;
//a few lines later:
icon:'http://www.google.com/mapfiles/marker.png?i='+(index++)
现在您可以选择用于标记的<img/>-元素,例如通过属性选择器实现 onmouseover/onmouseout。
当您不想使用原生 javascript 时,您可以使用 document.querySelector 来访问图像。
注意:您必须将标记的optimized-选项设置为false(这将强制API将标记呈现为单个元素)
Demo : http://jsfiddle.net/doktormolle/nBsh4/