Geocoder is a complete Geocoding solution for Ruby. With Rails, it adds Geocoding (by street or IP address), Reverse Geocoding (find street address based on given coordinates), and Distance Queries. It’s as simple as calling Geocode on your objects, and then using a scope.

The Geocode method sends a request to an external API, by default the Google Maps API. As it calls an external service it would be better to move this call into a background process.

Install Geoceder Gem

$ gem install geocoder

Simple Usage for Gem Geocoder

  1. Create New Apps.
  2. Add gem geocoder in Gemfile.
  3. Add method from geocoder in your model.


geocoded_by :address

Add Reverse method

reverse_geocoded_by :latitude, longitude

Add Callback

after_validations :geocode

Distance Location

You can add code for search distance between two location, example:

@location_first = Location.create(address: “Bandung, Jawa Barat”)
@location_second = Location.create(address: “Cimahi, Jawa Barat”)



Pretty Form for Search Location

For this field, I add jQuery for pretty search or Geocomplete.

Add library jQuery from Google Maps API, Ajax to your source and file jQuery Geocomplete

<script src=';libraries=places' ></script>
<script src='' ></script>
<%= javascript_include_tag "jquery.geocomplete" %>

Then you can use this jQuery syntax:

var options = {
          map: ".map_canvas"


Hope this article helpful for you.

Written by M. Luay – Developer at 41studio

Read more at the source