2013-11-03 104 views
1

我試圖應用Google Maps API v.3,它顯示了用戶在Ruby on Rails應用程序中經緯度的位置。但是,當我訪問一個地圖應該出現的頁面時,我得到了空白div,直到我刷新頁面。然後地圖顯示正確。這是我用的腳本。谷歌地圖API v3沒有正確顯示位置

(function() { 
    $(document).ready(function() { 
    var map, mapOptions, marker, myLatlng; 
    myLatlng = new google.maps.LatLng($("p#latitude").html(), $("p#longitude").html()); 
    mapOptions = { 
     zoom: 16, 
     center: myLatlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 
    return marker = new google.maps.Marker({ 
     position: myLatlng, 
     map: map, 
     title: "Ahoy!" 
    }); 
    }); 

此外,我包括<script src="http://maps.google.com/maps/api/js?sensor=false" style=""></script><script src="http://maps.gstatic.com/intl/en_us/mapfiles/api-3/14/11/main.js" type="text/javascript"></script>

可以採取什麼問題嗎?

我從Gemfile中刪除了gem turbolinks,並從application.js中刪除了'require turbolinks',現在它工作正常。

+0

是你的整個代碼? –

+0

對於js?當然。當然,所有這些都包含在html文檔中,其中有兩個容器「#latitude」和「#longitude」。但即使當我通過座標不與變量,但確切的數字 - 它只能在頁面刷新後才起作用。 – Shipitko

回答

0

你似乎已經回答了你自己的問題,但以防萬一別人看這件事:當你使用turbolinks的document.ready不會在頁面之間產生 - 從本質上講,turbolinks對待你的整個應用程序作爲一個頁面。

你的問題有兩種解決方案:一是爲了擺脫turbolinks,你做了什麼;或兩個,以將您的js附加到特定的turbolink事件,例如page:load。