admin管理员组文章数量:1417426
I have the following code and it correctly creates all the markers that I am storing but then when I click on any of them only the last InfoWindow is created and it only appears over the last marker no matter which marker I click on. I would imagine this has something to so with the same variable being used in my for loop.
{% for record in records %}
//need to do the JSON encoding because JavaScript can't have Jinja2 variables
//I need the safe here because Jinja2 tries to escape the characters otherwise
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);
});
//add the marker to the map
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 saw that worked for some other users on SO but then no InfoWindows show up. Are there any obvious errors here?
I have the following code and it correctly creates all the markers that I am storing but then when I click on any of them only the last InfoWindow is created and it only appears over the last marker no matter which marker I click on. I would imagine this has something to so with the same variable being used in my for loop.
{% for record in records %}
//need to do the JSON encoding because JavaScript can't have Jinja2 variables
//I need the safe here because Jinja2 tries to escape the characters otherwise
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);
});
//add the marker to the map
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 saw that worked for some other users on SO but then no InfoWindows show up. Are there any obvious errors here?
Share Improve this question asked Sep 10, 2012 at 15:55 clifgrayclifgray 4,42911 gold badges68 silver badges125 bronze badges1 Answer
Reset to default 5You need to create the markers in a separate function:
// Global var
var infowindow = new google.maps.InfoWindow();
and then, inside the loop:
var markerLatlng = new google.maps.LatLng(Lat, Lng);
var title = {{record.title|json_encode|safe}}
var iwContent = {{record.description|json_encode|safe}}
createMarker(markerLatlng ,title,iwContent);
Finally:
function createMarker(latlon,title,iwContent) {
var marker = new google.maps.Marker({
position: latlon,
title: title,
map: map
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(iwContent);
infowindow.open(map, marker);
});
}
Explanation here.
本文标签: javascriptHow to Create InfoWindows for Multiple Markers in a For loopStack Overflow
版权声明:本文标题:javascript - How to Create InfoWindows for Multiple Markers in a For loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745259292a2650281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论