当Polygon重叠时,RichMarker无法点击

时间:2014-08-06 21:01:34

标签: maps polygon marker

当Google地图(API v3)包含多边形时,放置在多边形旁边的标记是可点击的,但RichMarkers则不是。单击RichMarker时,重叠的Polygon会收到点击,但不会收到RichMarker。我错过了什么?

显示Polygon与标准Marker和RichMarker重叠的示例代码: http://jsfiddle.net/Hergott/1o15npd6/

var PathData = [
[49.2761419673641, -123.118069007778], 
[49.2791259862655, -123.129144031353],
[49.2704849721733, -123.125236002048],
[49.2732990317854, -123.117229946411],
[49.2761419673641, -123.118069007778]
];

function initialize() {

    var map = new google.maps.Map(document.getElementById('map-canvas'));
    var infowindow = new google.maps.InfoWindow();
    var bounds = new google.maps.LatLngBounds();

    //original marker
    var point1 = new google.maps.LatLng(49.272238, -123.122482);
    var marker1 = new google.maps.Marker({ position: point1, map: map,  title: 'Original marker' });
    google.maps.event.addListener(marker1, 'click', function() {
        infowindow.setContent(this.title);
        infowindow.open(map, this);
    });
    bounds.extend(point1);

    //RichMarker
    var point2 = new google.maps.LatLng(49.277, -123.125);
    var RichMarkerDiv='<div style="width:100px; height:100px;border:solid black 1px;">RichMarker</div>';
    var marker2 = new RichMarker({ position: point2, map: map,  title: 'RichMarker', content: RichMarkerDiv });
    google.maps.event.addListener(marker2, 'click', function() {
        infowindow.setContent(this.title);
        infowindow.open(map, this);
    });
    bounds.extend(point2);

    //polygon
    var path = [];
    for (var i in PathData) {
        var p = PathData[i];
        var latlng = new google.maps.LatLng(p[0], p[1]);
        path.push(latlng);
        bounds.extend(latlng);
    }
    var poly = new google.maps.Polygon({ paths: path, strokeColor: '#FF0000', strokeWeight: 3, fillColor: '#FF0000', fillOpacity: 0.1 });
    poly.setMap(map);
    google.maps.event.addListener(poly, 'click', function() {
        infowindow.setContent('Polygon clicked');
        infowindow.open(map, this);
    });    

    //fit map
    map.fitBounds(bounds);
}

initialize();

1 个答案:

答案 0 :(得分:1)

发现问题: RichMarker确实收到了点击,但由于它没有将事件传递给事件处理程序,它传播到了Polygon,Polygon也收到了点击。

解决方案: 1.修复RichMarker.js中错误的事件处理,第731-733行更改为     google.maps.event.addDomListener(this.markerContent_,&#39;点击&#39;,功能(e){
      google.maps.event.trigger(即,&#39;点击&#39;,e);     });

  1. RichMarker的点击监听器现在接收事件句柄并使用它来停止传播 google.maps.event.addListener(marker2,&#39;点击&#39;,功能(e){
        infowindow.setContent(&#39; RichMarker点击&#39;);     infowindow.open(map,this);     e.stopPropagation(); });