简体   繁体   中英

Google Maps API InfoWindow tied to last marker

I'm trying to add an info window to each marker I put on the map. The info window is only displaying the last added marker. I have tried the other solutions on here but none have been working. Currently I'm doing this with the following code:

(offending code starts halfway through at the comment //WORK IN PROGRESS)

 function placeMarker(data, map){ var lati, lngi, title, details; var image = 'image.png'; for(i=0; i < data.length; i++){ console.log(data[i].eventLocation + " OF TYPE " + data[i].eventType); if(data[i].eventName != ""){ title = data[i].eventName; } if(data[i].eventDetails != ""){ details = data[i].eventDetails; } if(data[i].eventLocation == "Location 1"){ lati = -29.651409; lngi = 82.342909; } else if(data[i].eventLocation == "Location 2"){ lati = -29.637712; lngi = 82.368024; } else if(data[i].eventLocation == "Location 3"){ lati = -29.650533; lngi = 82.342684; } else{ lati = -29.645793; lngi = 82.347717; console.log(data[i].eventLocation + " not found"); } // WORK IN PROGRESS var infowindow = new google.maps.InfoWindow({ content: details }); newMarker = new google.maps.Marker({ position: {lat: lati, lng: lngi}, title : title, map: map, icon: image, details: details }); newMarker.addListener('click', function() { //needs some work to connect to each marker instead of last one placed infowindow.open(map, newMarker); }); } } function initMap(data) { var uluru = {lat: -29.643220, lng: 82.350427}; var map = new google.maps.Map(document.getElementById('map'), { zoom: 14, center: uluru }); // Marker Images var centerMarker = new google.maps.Marker({ position: {lat: -29.643894, lng: 82.354748}, map: map, icon: image3 }); placeMarker(data, map); } 

Trying to find a solution has let me to displaying the last info entered on every marker, but not the correct info for the corresponding marker. I changed this to this:

 newMarker.addListener('click', function() { //needs some work to connect to each marker instead of last one placed infowindow.open(map, newMarker); }); 

 google.maps.event.addListener(newMarker, 'click', function() { //needs some work to connect to each marker instead of last one placed infowindow.open(map, this); }); 

Try placing it this way, defining this function before your loop:

var assignListener = function(markerObj, infoWindowObj){
                        return function() {
                               infoWindowObj.open(map, markerObj);
                                };
                      };

Then add the listeners via that function:

 newMarker.addListener('click', assignListener(newMarker, infowindow) );

try my modification.. I can't debug your end but let me know if that works... if not, I guess that will help you to see the way that I use to iterate the loop and then fill empty array with all marker info, details etc and attach that event to maps..

  var infoWindow = new google.maps.InfoWindow;
  // open info window
  var onMarkerClick = function() {
    var marker = this;
    infoWindow.setContent( marker.details );
    infoWindow.open(map, marker);
    mkmap.lastmarkeropened = marker;
  };
  // close the info window
  google.maps.event.addListener(map, 'click', function() {
    infoWindow.close();
  });

  var marker = [] ;
  // loop start
  for(i=0; i < data.length; i++){
    console.log(data[i].eventLocation + " OF TYPE " + data[i].eventType);
    if(data[i].eventName != ""){
      title = data[i].eventName;
    }
    if(data[i].eventDetails != ""){
      details = data[i].eventDetails;
    }

      if(data[i].eventLocation == "Location 1"){
        lati = -29.651409;
        lngi = 82.342909;
      }
      else if(data[i].eventLocation == "Location 2"){
        lati = -29.637712;
        lngi = 82.368024;
      }
      else if(data[i].eventLocation == "Location 3"){
        lati = -29.650533;
        lngi = 82.342684;
      }
      else{
        lati = -29.645793;
        lngi = 82.347717;
        console.log(data[i].eventLocation + " not found");
      }

      marker[ i ] = new google.maps.Marker({
        position: {lat: lati, lng: lngi},
        title : title,
        map: map,
        icon: image,
        details: details
      });

      // attach event to open info window
      google.maps.event.addListener(marker[ i ], 'click', onMarkerClick );
    } // end loop

see if this works..

cheers, K

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM