Google maps in Rails3 Application — Sample Application


Hi friends,

(New update: For new version(0.9.1) check Jay’s comment below)

(Update: this tutorial works only on gem version 0.7.7 and won’t work on latest gem version 0.8.7)

Using a gmaps4rails gem to display a map in our application

create a rails application using,

$ rails new gmaps

$ cd gmaps

Add this Gem in your Gemfile

gem ‘gmaps4rails’, ‘0.7.7’

Now bundle it ($ bundle install )and migrate with db ( $ rake db:migrate)

Create a scaffold using,

$ rails g scaffold Location name:string address:string longitude:float latitude:float

remove the longitude and latitude fields in _form.html.erb

After that, add this in app/view/layout

<%= yield :head %> (in your header) <%= yield :scripts %> (in your footer)

/app/views/layouts/application.html.erb

<html>
<head>
  <title>Googlemap</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
  <%= yield :head %>
</head>
<body>
<%= yield %>
<%= yield :scripts %>
</body>
</html>

In model /app/models/location.rb to add

class Location < ActiveRecord::Base
 acts_as_gmappable
      def gmaps4rails_address
          address
      end
       def gmaps4rails_infowindow
         "<h4>#{name}</h4>" << "<h4>#{address}</h4>"
     end
end

add the line in controller /app/controllers/locations_controller.rb

def index
    @locations = Location.all
    @json = Location.all.to_gmaps4rails
    ..........

In your view app/views/locations/index.html.erb to add a last line as

<%= gmaps4rails(@json) %>

then start the server(http://localhost:3000/locations) to put the name and address, longitude and latitude as set automatically, then will be show the markers as corresponding address and click the marker icon.it will shows infowindow as name and address

That’s it!! If you have any questions please feel free to contact me. I will be happy to help where I can. 🙂

Map View

My Sample Application:

http://gmaps4rails.heroku.com

GitHub:

https://github.com/thasuresh/gmaps_project

Ref Link:

http://www.youtube.com/user/TheApneadiving

30 thoughts on “Google maps in Rails3 Application — Sample Application

  1. Pingback: Google Maps World – ALL MAPS
  2. Hi! I follow the guide, and also the guild you linked. But all that happend when try to add something is “undefined method ‘gmaps’ for #

    I follow the screencast EXACTLY. any idea?

    • Hi Jimmy,

      Can you send me the “development.log” file. You can find it in the folder named “log” or The Error report shown on the localhost server.

      • Hi Jimmy,
        This tutorial explains gem version 0.7.7 ly. now they upgraded the “Gem Version” as o.8.7. when you add the gem in Gemfile, also add the version of the gem which you need

  3. I think, the problem is while you add the gem. So don’t just copy the line from my blog, type it instead. Because, for some reason the quotes used here are not accepted in Rails.

  4. Hi, thanks for the great guide. I have the same problem as Jimmy (followed the guide exactly and got the “undefined method ‘gmaps'” when I try to add a location).
    I did type the quotes instead of copy/pasting.
    I’m using rails 3.0.7 on Ubuntu.

    • Just a note: I’ve found out that it works if I use gmaps4rails-0.7.7 (the one you are using on your git repository, gmaps_project).

  5. Great application write up. I am trying to add an image to the info window based on the address -with no luck . Do I need to code the path in the database? Any help would be greatly appreciated.

    Thanks

  6. Great plugin! Works right out of the box for me with Rails 3.

    I’m new to Rails, and have a model with 4 different fields for address, city, state, and zip already in by DB.

    Is there a way to use this information with gmaps4rails? – Perhaps combine the four fields into one, and insert that into your address field?

  7. I got it working with 0.9.1.

    you just need to add t.boolean :gmaps in in migration definition, or so following scaffolding:

    rails g scaffold Location name:string address:string longitude:float latitude:float gmaps:boolean

    Thanks for the code.

  8. Hi, following the directions is too easy do a new app with gogglemaps. I ‘m trying to do the same but using hobo gem. Does any one try to use gmaps4rails with HOBO gem? Thanks for the help

  9. Pingback: Maps Google World - ALL MAPS – ALL MAPS
  10. Pingback: MAPS FLAGS » Google Maps World » MAPS FLAGS
  11. Pingback: MAPS FLAGS » Maps Google World » MAPS FLAGS
  12. in the step above where u said, “remove the longitude and latitude fields in _form.html.erb”…I don’t see where I’m supposed to remove them because this is the form fields where the user enters their lat/long. Am I missing something obvious?

  13. Hi Tha.Suresh,
    I am unable to load google map by following all instructions and after viewing all cooments.
    Please help me

    • hi tbhatti,

      Me also unable to load google map after that i try to find out the solution.

      first you change the gmaps4rails version lock link “gem ‘gmaps4rails’

      after in your model add column :

      acts_as_gmappable :latitude => ‘latitude’, :longitude => ‘longitude’, :process_geocoding => :geocode?,

      :address => “address”, :normalized_address => “address”,

      :msg => “Sorry, not even Google could figure out where that is”

      def geocode?

      (!address.blank? && (latitude.blank? || longitude.blank?)) || address_changed?

      end

      now you try google map is working.

      Ref: http://learningpoint2.wordpress.com/2012/12/19/google-map-in-rails/

  14. hi,
    iam manjunath
    i followed the all the instructions what u mentioned even though unable to load google map tell where i need to check to get google map

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s