jquery - 在AJAX上初始化Google map 刷新部分

标签 jquery ruby-on-rails ajax google-maps

我可以毫无问题地显示带有标记的谷歌地图,但是当使用ajax刷新该部分时,我无法在该部分中显示 map 。我猜这是因为使用 ajax 时不会触发最后的最终初始化行:

function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

var marker = new google.maps.Marker({
  position: myLatlng,
  map: map,
  title: 'Hello World!'
});
}

google.maps.event.addDomListener(window, 'load', initialize);

那么我可以使用 ajax 成功处理程序之一来初始化 map 吗?我尝试过,但没有成功,但语法可能是错误的,我不知道该把它放在哪里:

$(document).ajaxSuccess(function() {
    google.maps.event.initialize;
});

有什么想法吗?

编辑:

我正在 #response 中加载部分 _property_details.html.erb在properties/index.html.erb页面的div中添加 remote => true链接,这是 show.js.erb 代码:

properties/show.js.erb

$("#response").html("<%= escape_javascript(render('property_details')) %>");

Google代码全部在properties/index.html.erb的底部脚本标签中的页面。

最佳答案

假设您的部分刷新是使用 jQuery 完成的,这应该可以正常工作。

google.maps.event.addDomListener(window, 'load', initialize);
$(document).ajaxStop(initialize);

关于jquery - 在AJAX上初始化Google map 刷新部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16616463/

相关文章:

jquery - Rails/jquery 将 jQuery 中收集的变量(ID)传递给 Rails Controller /操作

javascript - 仅在不存在但没有 document.write 的情况下加载 jQuery

ruby-on-rails - 在不编辑源代码的情况下调用方法并从 Rails 控制台启动调试?

ruby-on-rails - Active Record 按最小值排序

jquery - jquery 自动完成中的文本字段锁定 - 仅允许在自动完成选项内提交

javascript - 如何在搜索框中为文本添加标签

javascript - 将元素附加到 div,但位于 div 内的另一个元素之前

ruby-on-rails - 无法激活activemodel-3.2.15,因为activesupport-4.0.1冲突

javascript - 电话间隙 : Cannot call remote hosts via HTTP

jquery - 为什么我从 jQuery UI 得到 "Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method ' close'"