【问题标题】:Clicking same Marker twice opens two InfoWindows单击相同的标记两次打开两个信息窗口
【发布时间】:2016-12-04 17:11:24
【问题描述】:

我让 infowindows 工作,但由于某种原因,如果我多次点击相同的标记,它会打开多个相同的 infowindow。我有一种感觉,它必须与我的代码有关,但我不能完全确定它是什么。任何帮助表示赞赏。

var map;
var markers = [];

function initMap() {
    map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: 14,
        center: new google.maps.LatLng(33.6894120, -117.9872660),
        mapTypeId: 'roadmap',
        disableDefaultUI: true
    });



    function addMarker(feature) {
        var marker = new google.maps.Marker({
            position: feature.position,
            icon: icons[feature.type].icon,
            map: map,
            type: feature.type,
            title: feature.title,
            description: feature.description
        });
        marker.addListener('click', function() {
            map.setCenter(marker.getPosition());

            var infoWindow = new google.maps.InfoWindow({
                map: map, 
                pixelOffset: new google.maps.Size(0, -60)
            });
            infoWindow.setContent(marker.description);
            infoWindow.setPosition(marker.position);

            google.maps.event.addListener(map, 'drag', function() {
                infoWindow.close();
            });
            google.maps.event.addListener(map, 'click', function() {
                infoWindow.close();
            });
        });
        markers.push(marker);
    }

    filterMarkers = function(getType) {
        //console.log(getType);
        for (var i = 0; i < markers.length; i++) {
            if (markers[i].type == getType || getType == "") {
                markers[i].setVisible(true);
            } else {
                markers[i].setVisible(false);
            }
        }
    }

    var features = [

        {
          position: new google.maps.LatLng(-33.91721, 151.22630),
          type: 'type1',
          description: 'Description1'
        },{
          position: new google.maps.LatLng(-33.91721, 151.22630),
          type: 'type2',
          description: 'Description2'
        },{
          position: new google.maps.LatLng(-33.91721, 151.22630),
          type: 'type3',
          description: 'Description3'
        }


    ];

    for (var i = 0, feature; feature = features[i]; i++) {
        addMarker(feature);
    }
}

$(document).ready(function(){
    initMap();
});

【问题讨论】:

  • 你能提供更多你的代码吗?您在哪里创建标记变量?
  • 我添加了 for 循环,您还需要什么吗?
  • 我的意思是您正在推送到标记数组,但我看不到您定义它的位置,这可能是您遇到此错误的原因。
  • Mert,我已经添加了我的完整代码。您是否看到任何可能是错误代码的内容?
  • 你能在initMap函数中定义map和markers变量然后再试一次吗?

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


【解决方案1】:

如果您不希望每次单击标记时都创建一个信息窗口,请不要在每次单击标记时都创建一个新的,为标记创建一个(或为地图创建一个,如果您只曾经想要一个打开),然后在点击监听器中打开它。

function addMarker(feature) {
  var marker = new google.maps.Marker({
    position: feature.position,
    map: map,
    type: feature.type,
    description: feature.description
  });
  // create infowindow for the marker 
  var infoWindow = new google.maps.InfoWindow({});
  marker.addListener('click', function() {
    map.setCenter(marker.getPosition());
    // set the content of the infowindow
    infoWindow.setContent(marker.description);
    // open the infowindow on the marker.
    infoWindow.open(map,marker);
  });
  markers.push(marker);
}

proof of concept fiddle

【讨论】:

    【解决方案2】:

    每次标记点击都会创建一个新的InfoWindow

    marker.addListener('click' ... var infoWindow = *new google.maps.InfoWindow( ...*

    预计您会获得多个实例。

    如果您想为所有标记使用一个InfoWindow,您可以关注this example

    如果您希望每个标记都有一个,请查看this SO answer

    【讨论】:

    • 这两个示例似乎都使用了单个内容字符串。我希望能够在我的标记数组中定义我的内容字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    相关资源
    最近更新 更多