How To Integrate A Static Google Map API In Your Rails Project

Versions used

Step 1: Set up

rails new “your_project_name"
rails g resource ExampleName
t.float “latitude” t.float “longitude” t.string “address”

Step 2: Geocode Gem

gem install 'geocoder’ 
gem ‘geocoder’
bundle install
geocoded_by :address
after_validation :geocode, if: :address_changed?

Step 4: Obtaining Google API

Step 5: Custome Credentials(optional but not really)

rails credentials:edit
EDITOR="name_of_editor --wait" rails credentials:edit
name_you_choose: "your_api_key"
Rails.application.credentials.name_you_chose

Step 6: Methods

def google_map(center)"https://maps.googleapis.com/maps/api/staticmap?center=#{center}&size=500x500&zoom=17&key=#{Rails.application.credentials.name_you_chose}"end
def google_map(center)"https://maps.googleapis.com/maps/api/staticmap?center=#{center}&size=500x500&zoom=17&key=[YOUR_API_KEY]"end
image_tag google(center: model_name.address)
image_tag google(center: [model_name.latitude, model_name.longitude].join(','))

Step 7: Markers

markers=size:small%7Ccolor:red%7Clabel:C%7C
"https://maps.googleapis.com/maps/api/staticmap?--><--center=#{center}&size=500x500&zoom=17&key=[YOUR_API_KEY]"

Conclusion:

Just existing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store