Friday, April 5, 2013

Add Markers to a Google Map With Ruby on Rails and JSON

September 25th, 2008 By: Wes

This tutorial will guide you through creating a map using the Google Maps API that will be dynamically populated with markers as the user zooms or scrolls around the map.
For this example, we’re going to create and use a generic Location model.

Geocoding Your Addresses

Geocoding will translate an address into its approximate latitude and longitude.
We’ll use GeoKit, a Ruby on Rails plugin to geocode our addresses. Install it with:
script/plugin install svn://
Follow the instructions to obtain and install your own Google API key.
Generate the model, controller and views for our map locations:
script/generate scaffold location name:string address:string city:string \
state:string zip:string
We also need to edit the migration file for this model and add fields for the location’s latitude and longitude:
t.decimal :lat, :precision => 15, :scale => 12
t.decimal :lng, :precision => 15, :scale => 12
Replace the code in app/models/location.rb with:
class Location < ActiveRecord::Base
  validates_presence_of :name, :address, :city, :state, :zip, :lat, :lng
  before_validation_on_create :geocode_address
    def geocode_address
      geo=GeoKit::Geocoders::MultiGeocoder.geocode("#{address} #{city} #{state} #{zip}")
      errors.add(:address, "Could not Geocode address") if !geo.success, self.lng =,geo.lng if geo.success
That’s all there is to geocoding, now any time we create a Location it will automatically be assigned a latitude and longitude.

Adding the Google Map

In app/views/locations/index.html.erb add:
<div id="map" style="width: 890px; height: 600px;"></div>
And in app/views/controllers/locations_controller.rb, change the index action to:
# GET /locations
# GET /locations.xml
# GET /locations.js
def index
  respond_to do |format|
    format.html do
      @locations = Location.find(:all)
    format.xml  { render :xml => @locations }
    format.js do
      ne = params[:ne].split(',').collect{|e|e.to_f}  
      sw = params[:sw].split(',').collect{|e|e.to_f}
      @locations = Location.find(:all, :limit => 100, :bounds => [sw, ne])
      render :json => @locations.to_json
The index action will now respond to javascript requests with a JSON object containing the first 100 Locations inside of the map boundaries.
In your layout file, add this code inside of the <head> tag:
<% unless @locations.blank? %>
    src="<%= GeoKit::Geocoders::google -%>"
  <%= javascript_include_tag 'prototype', 'maps' %>
<% end %>
And finally, create a public/javascripts/maps.js file with this code:
window.onunload = GUnload;
var map;
var markers = new Array();
Event.observe(window, 'load', function() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    // Center the map on the US
    map.setCenter(new GLatLng(37.731145,-97.326092),4);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
function updateMap() {
  var bounds = map.getBounds();
  var southWest = bounds.getSouthWest();
  var northEast = bounds.getNorthEast();
  // Send an AJAX request for our locations
  new Ajax.Request('/locations.js', {
    parameters: {sw: southWest.toUrlValue(), ne: northEast.toUrlValue()},
    onSuccess: function(transport){
      // Remove markers outside of our maps boundaries.
      if(markers.length > 0){
      // Add our new markers to the map (unless they are already on the map.)
      var json = transport.responseText.evalJSON();
      json.each(function(i) {
        id =;
        if(!markers[id] || markers[id] == null){
          // Marker doesnt exist, add it.
          markers[id] = createMarker(i.location);
function createMarkerClickHandler(marker, location) {
  return function() {
      '<div><strong>' + + '</strong><br/> ' +
      location.address + '<br/>' + + ', ' +
      location.state + ' ' + + '</div>'
    return false;
function createMarker(location) {
  var latlng = new GLatLng(, location.lng);
  var marker = new GMarker(latlng);
  var handler = createMarkerClickHandler(marker, location);
  GEvent.addListener(marker, "click", handler);
  return marker;
function removeMarkersOutsideOfMapBounds() {
  for(i in markers) {
    if(i > 0 && markers[i] && !map.getBounds().containsLatLng(markers[i].getLatLng())) {
      markers[i] = null;
The updateMap() function is run after the page initially loads and each time the user moves or zooms the map. It sends an AJAX request to the server with the maps boundaries, and the server returns a JSON object of the locations within those boundaries. After it receives the JSON object, it will add new locations to the map (it skips locations that have already been mapped) and removes locations that are no longer within the visible map boundaries.
A sample app containing all of the code can be downloaded here:

7 Responses to “Add Markers to a Google Map With Ruby on Rails and JSON”

  1. David H Says:
    Maybe I’m missing something … but I cant find the locations file in your sample code?
  2. Wes Bangerter Says:
    I’m not sure which location file you are referring to. I double checked and I’m pretty sure everything is in the sample code.
    The model, controller and views are in app/models/location.rb, app/controllers/locations_controller.rb and app/views/locations/ respectively.
  3. David H Says:
  4. Wes Bangerter Says:
    location.js is handled dynamically by Rails. When you access /locations.js it is ending up in app/controllers/locations_controller.rb and rendering the code in the format.js block on line 11.
    Basically, /location.js just returns a JSON string with all of the locations that should be displayed on the map, there isn’t any real code in it.
  5. David H Says:
    I get it now. Thank you for your replies and your great tutorial!
  6. James Says:
    Thanks for this! Every other article seems to suggest clearing and reloading ALL the markers which seems like overkill to me. This looks much simpler.
  7. Nauman Says:
    I m not ROR guru, but doing work in simple php. and want to create boundries around markers as
    Can anybody show me the sample result of above code that how it’s looking.

1 comment:

  1. Its nice.You can check mine too. Its regarding google maptags. Maptags shortens your address to your favourite word and makes sharing address as easy as sharing a word. Visit at