How to Geocode a Single Address Using a Ruby Library

This tutorial will show you how to use the .geocode() method of our Ruby Library to geocode a single address.

The Geocodio API is a great tool for integrating location-based data into your Ruby or Rails application. To help ease the work of developers, we’ve built a Ruby Library that includes a number of different methods that call the API and allow you to retrieve the information you require.

In this article, we’re going to go over how to implement and install our Ruby Library. Then, we’ll use it to geocode a single address.

On This Page

Part One: Installation

Start by navigating to your Gemfile. Once there, add the following gems:

gem 'geocodio-gem' gem 'faraday' gem 'faraday-follow_redirects'

The only requirement is geocodio-gem, but to prevent potential errors, you may also want to include faraday and faraday-follow_redirects if you haven’t already installed them. They work with the Geocodio Gem to make calls to the API.

Then, to ensure everything is loaded, run bundle install in your terminal.

Part Two: Configuration

At the top of the file where you’d like to work, add require ‘geocodio/gem’.

With the gem required, initialize a Geocodio::Gem class by passing in your API Key. If you still need to generate an API Key, you can do so by logging into your account and navigating to the API Keys tab in your account dashboard.

Note: You may want to consider securing your API Key in a .env file using a gem like dotenv or dotenv-rails.

geocodio = Geocodio::Gem.new(“YOUR API KEY”)

You now have access to a number of methods available through the Library, including .geocode(), .reverse(), .createList(), etc. You can call these methods on your newly created geocodio variable.

Part Three: Geocoding An Address

To forward geocode an address, use the .geocode() method and pass in a string that contains that address. Be sure to wrap the string in square brackets ([ ]). We’ll parse the address and return a JSON object that contains a pair of coordinates, as well as some additional information such as Accuracy Type and Score.

If you receive multiple results, the first result will be the most accurate to your input.

results = geocodio.geocode(["1109 N Highland St, Arlington, VA 22201"]) results # => {"input"=>{"address_components"=>{"number"=>"1109", "predirectional"=>"N", "street"=>"Highland", "suffix"=>"St", "formatted_street"=>"N Highland St", "city"=>"Arlington", "state"=>"VA", "zip"=>"22201", "country"=>"US"}, "formatted_address"=>"1109 N Highland St, Arlington, VA 22201"}, "results"=>[{"address_components"=>{"number"=>"1109", "predirectional"=>"N", "street"=>"Highland", "suffix"=>"St", "formatted_street"=>"N Highland St", "city"=>"Arlington", "county"=>"Arlington County", "state"=>"VA", "zip"=>"22201", "country"=>"US"}, "formatted_address"=>"1109 N Highland St, Arlington, VA 22201", "location"=>{"lat"=>38.886672, "lng"=>-77.094735}, "accuracy"=>1, "accuracy_type"=>"rooftop", "source"=>"Arlington"}]}

That’s all there is to it! You’ve made a basic call on a single address.

Don't have an account? Sign up to get 2,500 free lookups a day.

Create an API Key

API Documentation

See how to integrate geocoding, Census data, and more
Go to Docs

Metabase

Metabase is an easy, open source way for everyone in your organization to ask questions and learn from data.

Create an API Key

Create a free Geocodio API key
Copyright © 2014-2024 Dotsquare LLC, Norfolk, Virginia. All rights reserved.