我試圖應用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',現在它工作正常。
是你的整個代碼? –
對於js?當然。當然,所有這些都包含在html文檔中,其中有兩個容器「#latitude」和「#longitude」。但即使當我通過座標不與變量,但確切的數字 - 它只能在頁面刷新後才起作用。 – Shipitko