I am trying to create a Ruby on Rails application with Google Maps integration.
My source:
<!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>
Each time I load the screen I get a JavaScript error:
TypeError: null is not an object (evaluating 'a.offsetWidth')
Also, the map is not displayed. When I reload the page, the map finally appears and the JavaScript error vanishes.
Where is the problem and how do I fix it?