使用ajax v3的谷歌地图信息窗口
我已经差不多明白了,但还不太管用。我试图通过 ajax 加载内容,它确实加载了正确的内容,我可以看到它在 firebug 中加载,但是,它给了我一个错误:infowindow 未定义。我的 load_content
有点不对劲
在 load_content 之前的两行注释掉的行可以加载一个带有一些文本的小窗口。
function initialize() {
var myLatlng = new google.maps.LatLng(<%= @location_for_map.average(:lat) %>, <%= @location_for_map.average(:lng) %>);
var myOptions = {
zoom: 13,
center: myLatlng,
mapTypeControl: false,
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//var infowindow = new google.maps.InfoWindow();
setMarkers(map, places);
}
function setMarkers(map, locations) {
// Add markers to the map
for (var i = 0; i < locations.length; i++) {
var place = locations[i];
var myLatLng = new google.maps.LatLng(place.lat, place.lng);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: '/images/google_maps/numbers/'+(i+1)+'.png',
html: place.name,
id: place.id,
title: place.name
});
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', function() {
//infowindow.setContent(this.html);
//infowindow.open(map,this);
load_content(marker, this.id);
});
}
}
function load_content(marker, id){
$.ajax({
url: '/places/' + id + '.js',
success: function(data){
infowindow.setContent(data);
infowindow.open(map, marker);
}
});
}
function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
document.body.appendChild(script);
}
I've almost got it, but it doesn't quite work. I'm trying to load the content via ajax, and it does load the correct content, I can see it loading in firebug however, it gives me an error: infowindow not defined. I've got something out of place with load_content
The two commented out lines before load_content work to load a little window with some text.
function initialize() {
var myLatlng = new google.maps.LatLng(<%= @location_for_map.average(:lat) %>, <%= @location_for_map.average(:lng) %>);
var myOptions = {
zoom: 13,
center: myLatlng,
mapTypeControl: false,
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//var infowindow = new google.maps.InfoWindow();
setMarkers(map, places);
}
function setMarkers(map, locations) {
// Add markers to the map
for (var i = 0; i < locations.length; i++) {
var place = locations[i];
var myLatLng = new google.maps.LatLng(place.lat, place.lng);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: '/images/google_maps/numbers/'+(i+1)+'.png',
html: place.name,
id: place.id,
title: place.name
});
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', function() {
//infowindow.setContent(this.html);
//infowindow.open(map,this);
load_content(marker, this.id);
});
}
}
function load_content(marker, id){
$.ajax({
url: '/places/' + id + '.js',
success: function(data){
infowindow.setContent(data);
infowindow.open(map, marker);
}
});
}
function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
document.body.appendChild(script);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这有效:
This works:
您的 infowindow 声明仅在函数内部具有作用域。尝试声明具有全局作用域的 infowindow 变量。我相信这就是导致“信息窗口未定义”问题的原因。
Your infowindow declaration has scope only inside the function. Try declaring the infowindow variable with global scope. I believe that's what's causing the 'infowindow not defined' problem.
将此行移动
到任何函数之外,例如紧接在函数initialize(){}之后
Move this line:
to be outside any function, eg just after function initialize(){}