我几乎得到它,但它并不完全有效。我正在尝试通过 ajax 加载内容,它确实加载了正确的内容,我可以看到它在 firebug 中加载,但是它给了我一个错误:未定义信息窗口。我有一些不合适的地方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);
}