-1

I'm very grateful for the help in this Forum. So I am about to bring different groups of markers on the map and also use a toggle function, what works totally fine.

What do not work anymore are my eventlisteners for opening the markers info-windows.

When I run the following code and I klick on one of the markers, it always pops-up the info-window of the last marker of my array. So the mechanism works but there seems to be no iteration anymore.

I thought a lot about that problem but I didn't come to a conclusion. Thanks for a helping hand.

Here is the code

 var deutschland = new google.maps.LatLng(51,9);


 function initialize(){

  var mapOptions = { zoom: 6,
                     center: deutschland,
                     streetViewControl: true,
                     scaleControl: true,
                     mapTypeControl: true,
                     mapTypeId: google.maps.MapTypeId.ROADMAP }


 var map = new google.maps.Map(document.getElementById("map-container"),mapOptions);


 lat_data = [

 ['mid',50.0010400,9.0730700,'company1','marker.png','infowindow text 1'],
 ['mid',50.2010057,8.2592738,'company2','marker.png','infowindow text 2'],
 ['West',50.7430120,7.1264100,'company3','marker_green.png','infowindow text 3'], 
  ....

 ];



   markers = [];
   var i, newMarker;

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

    var infowindow = new google.maps.InfoWindow({
       content: lat_data[i][5]
   });


var shadow = new google.maps.MarkerImage('http://maps.google.com/mapfiles/shadow.png,
   new google.maps.Size(37,32),
   new google.maps.Point(0,0),
   new google.maps.Point(0,32));

var image = 
new google.maps.MarkerImage('http://maps.google.com/mapfiles/'+lat_data[i][4],
   new google.maps.Size(20,32),
   new google.maps.Point(0,0),
   new google.maps.Point(0,32));

 newMarker = new google.maps.Marker({
   position: new google.maps.LatLng(lat_data[i][1], lat_data[i][2]),
   map: map,
   title: lat_data[i][3],
   icon: image,
   shadow: shadow
 });

 newMarker.category = lat_data[i][0];
 newMarker.setVisible(true);

   google.maps.event.addListener(newMarker,'click',function(){
     infowindow.open(map,newMarker);

  });
  google.maps.event.addListener(map,'click',function(){
      infowindow.close(map,newMarker);
  });

 markers.push(newMarker);
 }

 }

function displayMarkers(category) {
  var i;

 for (i = 0; i < markers.length; i++) {
   if (markers[i].category == category) {
     if (markers[i].getVisible()) {
          markers[i].setVisible(false); }

    else { markers[i].setVisible(true); } } }

1 Answers1

0

Now I answer my own question (very cool, I never did that before!!!)

According to a tutorial of mozilla.org about closures I wrote the following code:

 .......
 iw = new google.maps.InfoWindow({
       content: lat_data[i][5]
   });

   function make_open_event_callback(iw){
       return function(){
          iw.open(map,this);
       };
   }
    function make_close_event_callback(iw){
       return function(){
          iw.close(map,this);
       };
   }

   google.maps.event.addListener(newMarker,'click',make_open_event_callback(iw));
  google.maps.event.addListener(map,'click',make_close_event_callback(iw));

 markers.push(newMarker);