【问题标题】:InfoWindow Not Displaying on click event点击事件时信息窗口不显示
【发布时间】:2012-09-19 04:18:20
【问题描述】:

所以右键单击会创建标记,但是当我单击标记时,信息窗口不会显示。注释警报 给出了坐标。我可能做错了什么。逻辑或语法是否有问题。我找不到解决这个问题的方法。这是我的代码:

   // 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);
            });

【问题讨论】:

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


    【解决方案1】:

    您没有通过调用 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

    【讨论】:

    • 我应该将点击事件嵌套在右键点击事件中。
    【解决方案2】:

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

    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);
        });
    });
    

    希望这会有所帮助。

    【讨论】:

    • 这正是我应该做的。你能解释一下为什么我应该嵌套它@paolo Rodrigues
    • 只是因为标记变量在已创建的上下文之外无法访问。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2014-08-10
    • 2012-05-25
    • 2012-07-13
    • 2019-01-10
    • 1970-01-01
    相关资源
    最近更新 更多