・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina
The following has been implemented.
・ Create Google Account Done. ・ Slim introduction
Google Cloud Platform
** ① Application restrictions **
Select None
.
** ② API restrictions **
Select Restrict Keys
and select Maps JavaScript API
from the pull-down menu.
** ③ Make sure that Maps JavaScript API
is selected and click Save
**
** ① Introduced "gem'dotenv-rails'" **
Gemfile
gem 'dotenv-rails'
Terminal
& bundle
** ② Create a ".env" file directly under the application **
Terminal
$ touch .env
** ③ Edit the .env
file **
.env
GOOGLE_MAP_API = 'Copyed API key' #Postscript
** ④ Edit the .gitignore
file **
.gitignore
/.env #Postscript
~html.slim
/View map
#map style='height: 500px; width: 500px;'
/Load API
- google_api = "https://maps.googleapis.com/maps/api/js?key=#{ ENV['GOOGLE_MAP_API'] }&callback=initMap".html_safe
script{ async src=google_api }
javascript:
let map;
function initMap() {
geocoder = new google.maps.Geocoder()
//Create a map
map = new google.maps.Map(document.getElementById('map'), {
//Specify the latitude and longitude of the location to be displayed in the center of the map
center: { lat: 40.7828, lng:-73.9653 },
zoom: 12,
});
//Specify the latitude and longitude of the place to put the marker
marker = new google.maps.Marker({
position: { lat: 40.7828, lng:-73.9653 },
map: map
});
}
If you do not disable turbolinks
, the map will not switch, so be sure to disable it.
Recommended Posts