谷歌地图仅在“刷新”后加载
Google map only loads after "refresh"
My Rails 4应用程序运行咖啡脚本来绘制Google地图。乍一看,它似乎不起作用。但是,当我点击浏览器刷新按钮时,地图像冠军一样加载。这已经过测试,并且正在发生:
- Mobile Safari (iOS7)
- 桌面浏览器 (OSX)
- 铬 (OSX)
- Chrome (Windows 7)
来自应用程序.html.erb 的标头
<head>
<!-- Boilerplate -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<%= stylesheet_link_tag "normalize.min.css" %>
<%= stylesheet_link_tag "main.css" %>
<%= javascript_include_tag "vendor/modernizr-2.6.2-respond-1.1.0.min.js" %>
<!-- Icon Font -->
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
<!-- Google Maps API -->
<%= javascript_include_tag "https://maps.googleapis.com/maps/api/js?key=*********&sensor=true" %>
<!-- Rails -->
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
<link href='http://fonts.googleapis.com/css?family=Droid+Sans|Lobster' rel='stylesheet' type='text/css'>
</head>
应用.js
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require jquery.turbolinks
//= require turbolinks
//= require_tree .
地点.js.咖啡
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
initialize = ->
mapOptions =
center: new google.maps.LatLng(33.51976, -101.95781)
zoom: 16
mapTypeId: google.maps.MapTypeId.ROADMAP
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions)
myLatlng = new google.maps.LatLng(33.51976, -101.95781);
marker = new google.maps.Marker(
position: myLatlng
map: map
title: "Hello World!"
)
contentString = "<div id='"info_content'">" + "<h3>Mighty Wash Amazing Autobath</h3>" + "<ul>" + "<li>6506 82nd Street, Lubbock, Texas 79424</li>" + "<li>806.553.0722</li>" + "<li>8:00am - 9:00pm</li>" + "</ul>" + "</div>"r
infowindow = new google.maps.InfoWindow(content: contentString)
google.maps.event.addListener marker, "click", ->
infowindow.open map, marker
infowindow.open(map, marker)
google.maps.event.addDomListener window, "load", initialize
位置/索引.html.erb
<div id="map-canvas"/>
这听起来很像 Rails javascript 只有在重新加载后才能工作。 建议的解决方案是用以下方法包装您的咖啡脚本:
ready = ->
// functions
$(document).ready(ready)
$(document).on('page:load', ready)
最简单的解决方法!
在用于访问带有地图的页面的链接上,只需添加数据无涡轮链接
例:
<a href="/contactuspage" data-no-turbolink>Contact Us</a>
问题解决了!我花了几天时间试图找到解决这个问题的简单方法。希望对您有所帮助!
这是一个复杂的问题,在 Rails 4 中使用 Turbolinks 时,需要缓存 javascript(现在默认为所有内部链接启用 Turbolinks)。
您可以通过在指向此页面的链接中指定'data-no-turbolink' => true
来强制加载没有涡轮链接的特定 URL。
例如,我在使用 gmaps4rails 插件时遇到了这个问题,并通过确保使用以下 url 链接到地图页面来解决这个问题:
link_to 'Map', maps_path, 'data-no-turbolink' => true
一般来说,Turbolinks似乎是Rails的一个有争议的补充,如果你不关心它提供的性能改进,你可以在全球范围内关闭它。
为菲利普·达夫尼的回应添加一些建议:
如果其他任何方法不起作用,请添加值"data: { no_turbolink: true }"到指向目标页面的链接。
例:
<%= link_to(locations, data: { no_turbolink: true } ) do %>
只需在关闭头标签之前添加此代码即可。或添加您的主题 ->自定义脚本部分:
jQuery(document).trigger('gmpBeforePrepareToDraw');
现在,您的地图已正确加载。 您无需再次刷新它。 :)
问候
- 单击即显示内容而不重新加载/刷新页面
- 在页面加载/刷新时在后台运行扩展
- vue.js使用定时器自动重新加载/刷新数据
- 在浏览器上显示之前,在后台预加载/刷新页面
- Firefox DevTools:选项卡和如何在页面重新加载/刷新后保持调试器文件打开?(停止自动关闭)
- 如何使命令按钮不重新加载/刷新页面
- 更改查询字符串而不重新加载/刷新
- 以 JSON 格式重新加载/刷新页面
- 使页面元素在重新加载/刷新后保持可见
- 使用 AJAX 调用重新加载/刷新 PHP 脚本
- 如何在通过Javascript连接到facebook API时重新加载/刷新类似facebook的按钮
- 使用Javascript加载刷新图像
- Javascript只有在重新加载/刷新后才能工作
- 在通过函数将数字转换为日期并加载刷新更多数据中
- 加载刷新页面后执行Javascript代码
- 随机HTML5背景视频页面加载/刷新
- 我的chrome扩展将不会加载,直到我重新加载/刷新相同的YouTube视频页面再次
- 我如何使一个html页面可打印而无需重新加载/刷新数据
- 脚本加载随机页面的页面加载/刷新
- 在页面加载/刷新时设置容器的默认内容,然后在单击事件触发后更改