Friday, May 17, 2024
188
rated 0 times [  194] [ 6]  / answers: 1 / hits: 23269  / 9 Years ago, sun, may 3, 2015, 12:00:00

I've got a working section of google maps javascript, I did have a problem.



Now the issue I had was that only one infowindow was showing up, the last. I found a solution on another stack thread that worked out. But I couldn't really work out why. I'm fairly new to Javascript so I was hoping someone could explain to me what changed and how.



Here is the working code:



function setMarkers(map, locations){
for(var i = 0; i < locations.length; i++){
var marker = locations[i];
var latLng = new google.maps.LatLng(locations[i][1], locations[i][2]);
var content = locations[i][0];
var infowindow = new google.maps.InfoWindow();

marker = new google.maps.Marker({
position:latLng,
map: map
});

google.maps.event.addListener(marker, 'click', function(content){
return function(){
infowindow.setContent(content);
infowindow.open(map, this);
}
}(content));
}
}


Here is the original broken code was (I'll paste only that which changed):



 google.maps.event.addListener(marker, 'click', function(){
infowindow.setContent(content);
infowindow.open(map, marker);
});


Now what I'd like to know if you'd be so kind, is:




  1. what function does the return fn serve, and


  2. what does the added (content) at the end of addListener (}(content));) do since as far as I can see the content has already been set within the setContent property.




Thank-you!


More From » google-maps-api-3

 Answers
234

Don't loop your infowindow...

You need only one instance of the infowindow object.

Move it outside of the loop, same for your functions.

Inside the loop assign its content as relative to the clicked marker




const locations = [
{lat: 45.840196, lng: 15.964331, name: Zagreb},
{lat: 43.514851, lng: 16.449083, name: Split},
{lat: 42.645725, lng: 18.094058, name: Dubrovnik},
];

function initGoogleMap(){

const infowindow = new google.maps.InfoWindow(); // Only one InfoWindow
const map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 6,
center: new google.maps.LatLng(45, 15)
});

function placeMarker( loc ) {
const marker = new google.maps.Marker({
position : new google.maps.LatLng( loc.lat, loc.lng ),
map : map
});
google.maps.event.addListener(marker, 'click', function(){
infowindow.close(); // Close previously opened infowindow
infowindow.setContent(`<div id=infowindow>${loc.name}</div>`);
infowindow.open(map, marker);
});
}

// ITERATE ALL LOCATIONS. Pass every location to placeMarker
locations.forEach( placeMarker );

}

google.maps.event.addDomListener(window, 'load', initGoogleMap);

html, body, #map-canvas { height: 100%; margin: 0px; }
#infowindow{ padding: 10px; }

<script src=https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk></script>
<div id=map-canvas></div>




[#66770] Friday, May 1, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mustafaericho

Total Points: 322
Total Questions: 103
Total Answers: 110

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
mustafaericho questions
Mon, May 31, 21, 00:00, 3 Years ago
Sun, May 23, 21, 00:00, 3 Years ago
Sat, Feb 13, 21, 00:00, 3 Years ago
Sat, Jan 2, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
;