3

我对 InfoWindow 有疑问。我有一个通过 JSON 检索数据的 ajax 函数,但是当您打开另一个时,我无法自动关闭 InfoWindow。我的代码是这样的:

var mapOptions = {
    center: new google.maps.LatLng(44.49423583832911, 11.346244544982937),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("mappa_locali"),mapOptions);
$.ajax({
    type:'GET',
    url:"locali_json.php"+urlz,
    success:function(data){ 
          var json = JSON.parse(data);
          for (var i=0; i<json.length; i++) {
             point = new google.maps.LatLng(json[i].latitudine,json[i].longitudine);
             var infowindow = new google.maps.InfoWindow;
             infowindow.setContent('<a href="./dettaglioLocale.php?id_loc='+json[i].id_locale+'">'+json[i].nome_locale+'</a><br>'+json[i].address);
             addMarkerz(point,infowindow);
          }
    }
})
 }


   function addMarkerz(point,infowindow) { 
    position: point,
    map: map
   });
    google.maps.event.addListener(marker,'mouseover',infoCallback(infowindow, marker));
    markers.push(marker);
    infos.push(infowindow);
     }

    function infoCallback(infowindow, marker) { 
      return function() {
         infowindow.close();  
        infowindow.open(map, marker);

      };
    }

有谁知道哪里减少了错误?或者你对我有什么建议吗?

4

3 回答 3

9

建议只创建一个信息窗口(在全局范围内),在单击标记时重用它并更改其内容,如果用户单击地图则关闭它。

代码片段(删除对 AJAX 调用的依赖):

// global variables
var map;
var markers = [];
var infowindow = new google.maps.InfoWindow();

function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(44.49423583832911, 11.346244544982937),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("mappa_locali"), mapOptions);
  var json = JSON.parse(data);
  for (var i = 0; i < json.length; i++) {
    point = new google.maps.LatLng(json[i].latitudine, json[i].longitudine);
    var infowindowHtml = '<a href="./dettaglioLocale.php?id_loc=' + json[i].id_locale + '">' + json[i].nome_locale + '</a><br>' + json[i].address;
    addMarkerz(point, infowindowHtml);
  }
}

function addMarkerz(point, infowindowHtml) {
  var marker = new google.maps.Marker({
    position: point,
    map: map
  });
  google.maps.event.addListener(marker, 'mouseover', infoCallback(infowindowHtml, marker));
  markers.push(marker);
}

function infoCallback(infowindowHtml, marker) {
  return function() {
    infowindow.close();
    // update the content of the infowindow before opening it
    infowindow.setContent(infowindowHtml)
    infowindow.open(map, marker);

  };
}


var data = JSON.stringify([{
  latitudine: 44.494887,
  longitudine: 11.3426163,
  id_locale: "0",
  nome_locale: "Bologna",
  address: "Bologna, Italy"
}, {
  latitudine: 44.4946615,
  longitudine: 11.314634999999953,
  id_locale: "0",
  nome_locale: "Quartiere Saragozza",
  address: "Quartiere Saragozza, Bologna, Italy"
}]);


google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#mappa_locali {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="mappa_locali"></div>

于 2013-01-16T13:46:14.413 回答
5
function infoCallback(infowindow, marker) { 
  return function() {
     infowindow.close();  
    infowindow.open(map, marker);

  };
}

应该改为

function infoCallback(infowindow, marker) { 
    return function() {
        //Close active window if exists
        if (activeWindow != null) {
            activeWindow.close();
        }
        //Open new window 
        infowindow.open(map,marker); 
        //Store new window in global variable 
        activeWindow = infowindow; 
    };
}

并在 .js 文件中将 activeWindow 声明为全局变量var activeWindow

于 2013-10-01T11:37:53.470 回答
1

我想我有一个简单的解决方案。我只是保存最后一个打开的标记。在下一次单击时,首先我关闭最后一个标记。

var markers = [];
var infowindows = [];

function addMarkes(facilityID) {

        var bounds = new google.maps.LatLngBounds();

        for (var i = 0; i < variants.length; i++) {

            var v = variants[i];

            var position = new google.maps.LatLng(v.Lat, v.Long);

            bounds.extend(position);

            markers[i] = new google.maps.Marker({
                position: position,
                title: v.Title,
                map: map,
                animation: google.maps.Animation.DROP,
                icon: v.Icon,
                infoWindowIndex: i //synchronize with infoWindows
            });

            var localContent = '<div><span class="address">' + v.Address + '</span></div>';

            infowindows[i] = new google.maps.InfoWindow({
                content: localContent
            });

            //Just for multiple marks. 
            if (variants.length > 0) {
                var lastOpen = -1; //Save the last open mark
                google.maps.event.addListener(markers[i], 'click', function (innerKey) {
                    return function () {
                        //Close the last mark.
                        if (lastOpen > -1) {
                            infowindows[lastOpen].close();
                        }
                        infowindows[innerKey].open(map, markers[innerKey]);
                        lastOpen = innerKey;
                    }
                }(i));
            }
        }

        map.fitBounds(bounds);

    }
于 2014-10-21T00:06:45.633 回答