Google Maps for Rails with Gmaps4 Rails: Tutorial on how to post and filter locations

Andy Glass

In this blog post, I’ll review how to geocode and map locations using Gmaps4rails, an awesome gem from Benjamin Roth. I’ll also show you how to use javascript to create filters to show/hide markers using data from your model.

Lets get started

Installing Gmaps4rails
In your Gemfile, add the gem

gem 'gmaps4rails'

Bundle and from your commandline, instruct the below to generate the Gmaps4rails coffeescript files.

$ rails generate gmaps4rails:install

Gmaps4rails works by making a rails model ‘gmappable’. The data I’ll be using for this demo is based on a city model. Lets create the migration:

class CreateCities < ActiveRecord::Migration
  def change
    create_table :cities do |t|
      t.string :name
      t.string :state
      t.float :latitude
      t.float :longitude
      t.boolean :gmaps
      t.integer :population
      t.timestamps
    end
  end
end

To make the model ‘gmappable’, on the model:

acts_as_gmappable

Gmaps4rails has an optional geocoding feature which calculates the lat and long needed to plot a location. We’ll…

View original post 1,450 more words

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s