Rails 5. , , , , .
, script. ( js (script) /app/assets/javascript.
@location, latitude longitude.
div id = #location-longitude ( HAML #location-longitude). div id = #location-latitude. div id javascript, , div. (, XML). , Google,
( , , HAML , javascript. , HAML .)
/ page title
%h1 Location with Dynamic Map
%h3 Coordinates:
/ this div id is important. The javascipt below looks for it and gets the inner value (= the value of @location.latitude )
#location-latitude
#{@location.latitude}
/ this div id is also used by the javascript below
#location-longitude
#{@location.longitude}
%h3 Dynamic Google Map:
/ this div id is where the javascript below will put the map
#map
%script{ type: 'text/javascript'}
var map;
function initMap() {
var lat_value = Number(document.getElementById('location-latitude').childNodes[0].nodeValue);
var long_value = Number(document.getElementById('location-longitude').childNodes[0].nodeValue);
var coordinates = {lat: lat_value, lng: long_value};
map = new google.maps.Map(document.getElementById('map'), {
center: coordinates,
zoom: 11
});
var marker = new google.maps.Marker({
position: coordinates,
map: map
});
}
%script{ defer: 'async', src:"https://maps.googleapis.com/maps/api/js?key=#{YOUR_GOOGLE_API_KEY}&callback=initMap"}