Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

google maps api 3 - close InfoWindow before open another

I have a problem with InfoWindow. I have an ajax function that retrieves data via JSON, but I can not get close InfoWindow automatically when you open another. My code is this:

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

      };
    }

Does anyone know where decreased mistake? Or do you have any advice for me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The suggestion is only create one infowindow (in the global scope), reuse it and change its contents when a marker is clicked, close it if the user clicks on the map.

code snippet (removes the dependency on the AJAX call):

// 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...