We forked this version (1.5.6) of apneadiving’s gem because it handles the things we need the most - displaying Google Maps and Google geocoding. However, if you have a Google Maps for Business account, your geocoding requests have to be signed. I’ve modified this gem so that it handles the signing. Here is the setup:
1) In your project, modify gmaps4rails.googlemaps.js.coffee and add the client id to @map_options, i.e.:
client: “<client id goes here>”
You could also modify that file to grab the client ID another way, but I didn’t feel it was worth the effort for our use case. The client ID does not need to be protected because it only works via authroized URLs.
2) Add the following environment variables:
GMAPS_API_CLIENT_ID=<client id goes here>
GMAPS_API_CRYPTO_KEY=<crypto key goes here>
That’s pretty much it. Now your Google Maps Javascript API v3 map requests will use your client ID, and your Google Geocoding API requests will use your client ID and will be signed properly.
If you use this gem for Google driving directions or places, you are on your own.
I did modify base_net_methods.rb which may break those two implementations, but like I said I haven’t tested to be sure. If they are broken I only changed one line, probably a fairly trivial fix.
The remainder of thise readme is from apneadiving’s original repo.
Google Maps for Rails (gmaps4rails) <img src=“https://secure.travis-ci.org/apneadiving/Google-Maps-for-Rails.png?branch=master” alt=“Build Status” /> <img src=“https://codeclimate.com/badge.png” />¶ ↑
Gmaps4rails is developed to simply create a Google Map:
-
directly from your model,
-
from your own json
It’s based on Ruby on Rails 3 Engines and uses Google Maps API V3.
See (somewhat outdated) screencasts here: www.youtube.com/user/TheApneadiving
I’ve added support for other map providers: openlayers, mapquest & bing.
See: github.com/apneadiving/Google-Maps-for-Rails/wiki/Map-Apis
Any help would be appreciated to complete this work.
1) Gemfile
gem 'gmaps4rails'
2) Copy the assets to your app (and be sure to copy the fresh assets when you upgrade the gem):
rails generate gmaps4rails:install
This will copy all the coffeescript files if you’re running Rails >= 3.1 or all the js files for Rails 3.0.x.
Only ‘gmaps4rails.base` + `gmaps4rails.your_provider` are necessary.
3) Javascript
<%= yield :scripts %> (in your footer)
This container will include the js files from map providers, the custom js to display the map, the gem’s js code.
4) CSS
‘gmaps4rails.css` will be copied to your app after you run the Rails generator. Be sure to require this file in your view for your first steps.
For Rails 3.0.x or without assets pipeline:
<%= stylesheet_link_tag 'gmaps4rails' %>
For Rails >= 3.1 with assets pipeline, same principle: add the required file to your manifest.
In your model, add:
acts_as_gmappable def gmaps4rails_address #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki "#{self.street}, #{self.city}, #{self.country}" end
Create a migration and add the following fields to your table (here users):
add_column :users, :latitude, :float #you can change the name, see wiki add_column :users, :longitude, :float #you can change the name, see wiki add_column :users, :gmaps, :boolean #not mandatory, see wiki
Mongoid example:
acts_as_gmappable :position => :location field :location, :type => Array
In your controller:
@json = User.all.to_gmaps4rails
In your view:
<%= gmaps4rails(@json) %>
Done!
-
Markers with Info window, Custom Picture, RichMarkers (make your own markers with custom html)
-
Automatic sidebar with list of markers
-
Circles, Polylines, Polygons
-
Geocode directly your address and retrieve coordinates.
-
Wrapper for ‘Direction’, giving instructions to go from point A to point B
-
Auto-adjust the map to your markers
-
Refresh your map on the fly with Javascript (and Ajax)
-
KML support
-
Easy multimap
Feel free to contact us, you have your say.
This is how you can launch the spec suite (js + ruby):
-
Clone the gem repository
-
Go to the gem’s folder
-
run ‘bundle`
-
run ‘cd spec/dummy; rake db:migrate RAILS_ENV=test; cd ../..`
-
run ‘bundle exec rspec spec`
MIT license.
Authors: Benjamin Roth, David Ruyer