我正嘗試使用Google地圖集成創建Ruby on Rails應用程序。Ruby on Rails中的Google地圖JavaScript錯誤
我來源:
<!DOCTYPE html>
<html>
<head>
<title>Website</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
function initialize() {
var myLatlng = new google.maps.LatLng(56.794682, 25.224593);
var mapOptions = {
zoom: 15,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'This is a location'
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<script type="text/javascript">
ready = function() {
//Current page ID
var page_id = $('div.content').attr('id');
//Navigation list of all pages
var navigation_list = $("nav#navigation a");
//Navigation list check
navigation_list.each(function(a){
if ($(this).attr('id') == page_id) {
$(this).css("border-bottom", "3px solid #74c5bd");
}
});
}
$(document).ready(ready)
$(document).on('page:load', ready)
</script>
</head>
<body>
<%= render "layouts/header" %>
<%=yield %>
<%= render "layouts/footer" %>
</body>
</html>
每次我打開屏幕,我得到一個JavaScript錯誤:
TypeError: null is not an object (evaluating 'a.offsetWidth')
而且,不顯示地圖。當我重新加載頁面時,地圖終於出現,並且JavaScript錯誤消失。
問題在哪裏,我該如何解決?
哪裏是你的'div.content'? – omarvelous
'<%=yield %>'渲染它。 –
[Google MAP API Uncaught TypeError:無法讀取null的屬性'offsetWidth']的可能重複(http://stackoverflow.com/questions/11740663/google-map-api-uncaught-typeerror-cannot-read-property-offsetwidth -of-null) – dimakura