2013-10-04 116 views
1

我的Rails應用程序4運行腳本咖啡畫一個谷歌地圖。乍一看,它似乎不起作用。但是,當我點擊瀏覽器刷新按鈕時,地圖加載就像一個冠軍。這已經過測試,是發生在:谷歌地圖只加載後「刷新」

  • 移動Safari瀏覽器(iOS7)
  • 桌面Safari瀏覽器(OSX)
  • 鉻(OSX)
  • 瀏覽器(Windows 7)中

來自application.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> 

的application.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 . 

locations.js.coffee

# 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 

位置/ index.html.erb

<div id="map-canvas"/> 

回答

1

這是一個複雜的問題,有什麼用Rails 4使用Turbolinks時候做的JavaScript的緩存(Turbolinks現在在默認情況下所有的內部連接啓用)。

您可以強制特定的URL通過這個鏈接頁面指定'data-no-turbolink' => true沒有turbolinks加載。

例如,我跑進使用gmaps4rails插件這一問題,並確保我鏈接到地圖頁面使用以下URL解決它:

link_to 'Map', maps_path, 'data-no-turbolink' => true

Turbolinks似乎是一個有爭議的另外一般來說Rails,如果你不關心它提供的性能改進,你可以在全球範圍內關閉它。

4

最簡單的解決!

在使用與地圖訪問您的網頁的鏈接,只需添加數據沒有的TurboLink

例如:

<a href="/contactuspage" data-no-turbolink>Contact Us</a> 

問題解決了!我花了好幾天的時間試圖找到解決這個問題的簡單方法。希望能幫助到你!

0

添加一點忠告菲利普Duffney響應:

如果什麼都不能正常工作,增加價值「的數據:{no_turbolink:真正}」鏈接通向目標頁面。

例子:

<%= link_to(locations, data: { no_turbolink: true }) do %> 
-1

只是增收標記前這段代碼。現在

jQuery(document).trigger('gmpBeforePrepareToDraw');

你的地圖是否正確裝入:>自定義腳本部分 - 或者在你的主題添加。你不需要再刷新它。 :)

Regards