Create a new application
rails new explorcity
Let create two models ie City and Destination. City has an attribute name. Destination has attributes like name, latitude, longitude and city_id.
Let us go ahead and scaffold the two models
rails generate scaffold City name:string
rails generate scaffold Destination name:string latitude:float longitude:float city_id:integer
Creating the tables
rake db:migrate
Add Associations
Create a couple of cities and destinations and associate the data. We want to have markers of destinations on city’s show page.
Google Maps API
- Visit https://developers.google.com/maps/web/ for web API
- We want to embed maps onto our view file. For that visit https://developers.google.com/maps/documentation/embed/guide
-
- The above code will embed destination on our app. It takes latitude and longitude as queries
- As the API mentions, we need an API key. For the API key, you need to sign in with your Google Account
- To create, receive and enable the API key, visit https://console.developers.google.com/apis/dashboard
- Create a new project, enable the Google Maps API
- The following code specifies the destination
- This is how you embed a Map inside Rails application