Search code examples
ruby-on-railsgoogle-mapsrails-geocoder

Ruby on Rails - Geocoder view


Im trying to use geocoder for displaying a location based on an address.

My question is, what do I put in the view to show an interactive map once I got the Lat and Long from the address the user inserted in the database?

my address model

 belongs_to <another model>
 attr_accessible <all addresses attributes including latitude and longitude>

 geocoded_by :address

 after_validation :geocode, :if => :address_changed?

The fields Latitude and Longitude are automatically calculated after I insert the address.

I've followed Ryan's Rail Cast tutorial but at the end, instead of displaying a static image I want to display a map, google maps interactive style. How can I achieve this?

If've taken a look at Google-Maps-for-Rails but couldn't understand how very well like how to get a single map on location in the show view.


Solution

  • Take a closer look at Google-Maps-for-Rails. That gem makes it trivial, and the instructions on how to get the interactive map in your view are quite clear:

    (I'll presume your Address model belongs_to a User model. Change names as appropriate.)

    If you just want the one specific user on the map, of course you could change the controller:

    @json = User.find(params[:id]).address.to_gmaps4rails
    

    In your view:

    <%= gmaps4rails(@json) %>