【发布时间】:2011-05-02 01:34:03
【问题描述】:
我正在实现 Google Maps API,并且我希望在模板首次呈现时打开第一个标记的 InfoWindow,但仅在特定条件为真时打开。
我有这样的事情:
{% if project %}
//the following is automatically open the infowindow of the FIRST marker in the array when rendering the template
var infowindow = new google.maps.InfoWindow({
maxWidth:500
});
infowindow.setContent(markers[0].html);
infowindow.open(map, markers[0]);
{% endif %}
这不会在 Firefox 或 Internet Explorer 7 中引发错误;它做了我想要的——但它似乎是错误的。我的文本编辑器因警告/错误而尖叫。
这是不好的编码习惯吗?如果是这样,对替代方案有什么建议吗?
这是完整的代码,在脚本标签内,不相关的部分被删除:
function initialize() {
...
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var markers = []
setMarkers(map, projects, locations, markers);
...
}
function setMarkers(map, projects, locations, markers) {
for (var i = 0; i < projects.length; i++) {
var project = projects[i];
var address = new google.maps.LatLng(locations[i][0],locations[i][1]);
var marker = new google.maps.Marker({
map: map,
position:address,
title:project[0],
html: description
});
markers[i] = marker;
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(this.html);
infowindow.open(map,this);
});
}
{% if project %}
//the following is automatically open the infowindow of the FIRST marker in the array when rendering the template
var infowindow = new google.maps.InfoWindow({
maxWidth:500
});
infowindow.setContent(markers[0].html);
infowindow.open(map, markers[0]);
{% endif %}
})
}
google.maps.event.addDomListener(window, 'load', initialize);
【问题讨论】:
标签: javascript django google-maps coding-style django-templates