I have the following code, and it correctly creates all the tokens that I store, but then when I click on any of them, only the last InfoWindow is created, and it appears only above the last marker, regardless of which marker I click on. I would suggest that this is due to the fact that the same variable is used in my for loop.
{% for record in records %}
var GPSlocation = {{record.GPSlocation|json_encode|safe}};
var LatLng = GPSlocation.replace("(", "").replace(")", "").split(", ")
var Lat = parseFloat(LatLng[0]);
var Lng = parseFloat(LatLng[1]);
var markerLatlng = new google.maps.LatLng(Lat, Lng);
var marker = new google.maps.Marker({
position: markerLatlng,
title: {{record.title|json_encode|safe}}
});
var infowindow = new google.maps.InfoWindow({
content: "holding..."
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent({{record.description|json_encode|safe}});
infowindow.open(map, marker);
});
marker.setMap(map);
{% endfor %}
I tried changing the event listener to this:
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent({{record.description|json_encode|safe}});
infowindow.open(map, this);
});
As I understand it, this worked for some other users on SO, but then there is no InfoWindows. Are there any obvious errors here?
source
share