InfoWindow在点击事件中不显示

时间:2012-09-27 11:30:13

标签: google-maps-api-3 google-maps-markers infowindow

因此,右键单击会创建标记,但是当我单击标记时,infowindow不会显示。 注释警告 会给出坐标。我能做错什么?逻辑或语法是否存在问题。我无法找到解决这个问题的方法。这是我的代码:

   // create marker on right click
            google.maps.event.addListener(map,'rightclick', function(e) {

                    marker = new google.maps.Marker({
                    position: e.latLng,
                    map: map
                });

                alert('coord: ' + marker.getPosition().toUrlValue(3));

            });

            // display info window on marker click
            google.maps.event.addListener(marker,'click', function(event){

                infowindow =  new google.maps.InfoWindow({
                map:map, 
                content:"coordinates:"+event.latLng.toUrlValue(),
                position:event.latLng
                });

                infowindow.open(map,marker);
            });

2 个答案:

答案 0 :(得分:1)

您应该将第二个事件放在第一个事件的相同上下文中:

google.maps.event.addListener(map,'rightclick', function(e) {
    var marker = new google.maps.Marker({
        position: e.latLng,
        map: map
    });

    google.maps.event.addListener(marker,'click', function(event){
        infowindow =  new google.maps.InfoWindow({
            map: map, 
            content: "coordinates:"+event.latLng.toUrlValue(),
            position: event.latLng
        });

        infowindow.open(map, marker);
    });
});

希望这有帮助。

答案 1 :(得分:0)

你没有通过调用infowindow对象的open()来打开infowindow。每次单击标记时都会初始化。

<强>已更新

infowindow =  new google.maps.InfoWindow({
     map:map, 
     content:"coordinates:"+event.latLng.toUrlValue()
});

// display info window on marker click
google.maps.event.addListener(marker, 'click', function(event) {
   infowindow.setPosition(event.latLng);
   infowindow.open(marker);
});

试试这段代码

https://developers.google.com/maps/documentation/javascript/overlays#InfoWindows