# Geocoder

**URL:** <https://www.ruby-forum.com/t/geocoder/185299>\
**Category:** Ruby\
**Created:** [March 17, 2010, 5:55pm UTC](https://www.ruby-forum.com/t/geocoder/185299 "2010-03-17T17:55:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 17, 2010, 5:55pm UTC](https://www.ruby-forum.com/t/geocoder/185299/1 "2010-03-17T17:55:03Z")

</div>

I am trying to write a script that uses the rails-geocoder gem to grab  
the latitude and longitude so I can store those values in my own local  
table, but am having trouble getting anything to work or to find any  
decent documentation describing or even helping me accomplish what I  
need done. Does anybody have any suggestions? I have found this example:  
[http://geocoder.rubyforge.org/](http://geocoder.rubyforge.org/) that seems pretty straight forward. I  
have a list of cities and states and am just trying to loop through each  
and get the lat lon values. Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 19, 2010, 7:29pm UTC](https://www.ruby-forum.com/t/geocoder/185299/2 "2010-03-19T19:29:39Z")

</div>

Shandy N. wrote:

> I am trying to write a script that uses the rails-geocoder gem to grab  
> the latitude and longitude so I can store those values in my own local  
> table, but am having trouble getting anything to work or to find any  
> decent documentation describing or even helping me accomplish what I  
> need done. Does anybody have any suggestions? I have found this example:  
> [http://geocoder.rubyforge.org/](http://geocoder.rubyforge.org/) that seems pretty straight forward. I  
> have a list of cities and states and am just trying to loop through each  
> and get the lat lon values. Thanks,
> 
> -S

I think that geocoder is for full addresses. For just cities, you could  
use worldkit. Sending a request like so:  
[http://brainoff.com/worldkit/geocoder/rest/?city=San+Diego,CA,US](http://brainoff.com/worldkit/geocoder/rest/?city=San+Diego,CA,US)

will get you a response like this with the lat & long for the city:  
rdf:RDF  
geo:Point  
geo:long-117.191848\</geo:long\>  
geo:lat32.751575\</geo:lat\>  
\</geo:Point\>  
\</rdf:RDF\>

You have to make sure there are no spaces in the city field. So cycling  
through your list of cities, you’d make sure to concatenate multi word  
cities with a ‘+’ before sending the request.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 20, 2010, 12:02am UTC](https://www.ruby-forum.com/t/geocoder/185299/3 "2010-03-20T00:02:39Z")

</div>

My last post wasn’t actually relevant to ruby so here’s a little snippet  
using ruby, sqlite and worldkit. I’m just learning ruby myself so this  
might not be the best way to do this.

require ‘open-uri’  
require ‘sqlite3’

# make a table with two records, Dallas, TX and Lansing, MI

database = SQLite3::Database.new( “cities.database” )  
database.execute( “create table city\_names (id INTEGER PRIMARY KEY, name  
TEXT, state TEXT, country TEXT, latitude NUMERIC, long NUMERIC);”)  
database.execute( “insert into city\_names (name, state, country) values  
(‘Dallas’, ‘TX’, ‘US’)”)  
database.execute( “insert into city\_names (name, state, country) values  
(‘Lansing’, ‘MI’, ‘US’)”)

class Geocoder  
def initialize(db)  
@db = db  
@rows = db.execute( “select \* from city\_names”)  
end

def update\_coords  
@link = “”  
@rows.each do |item|  
@link = “#{item[1]},#{item[2]},#{item[3]}”  
open(“[http://brainoff.com/worldkit/geocoder/rest/?city=#{@link}](http://brainoff.com/worldkit/geocoder/rest/?city=#%7B@link%7D)”)  
do |f|  
sd = f.read  
@long = sd.scan(/long\>(._)\</)  
 @lat = sd.scan(/lat\>(._)\</)  
end  
@db.execute( “UPDATE city\_names SET long =‘#{@long[0][0]}’ WHERE  
id=#{item[0]}”)  
@db.execute( “UPDATE city\_names SET latitude =‘#{@lat[0][0]}’ WHERE  
id=#{item[0]}”)  
end  
end  
end

geo = Geocoder.new(database)  
geo.update\_coords

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 20, 2010, 9:24am UTC](https://www.ruby-forum.com/t/geocoder/185299/4 "2010-03-20T09:24:30Z")

</div>

Hi, here is an ActiveRecord based solution [gist:338506 · GitHub](http://gist.github.com/338506)  
It  
is based on Peter S.'s solution. It also populates most of the 50  
cities  
found at [Profiles of the 50 Largest Cities of the United States](http://www.infoplease.com/ipa/A0108477.html)

Here is the same thing, but using the geocoder gem that Shandy asked  
about

> <https://gist.github.com/JoshCheek/338523>

I also added some use cases for it that work with the data it populates  
into  
the db, such as  
dallas.distance\_to austin  
texas\_cities = City.find\_all\_by\_state(‘TX’).map { |city| city.lat\_lon }  
Geocoder.geographic\_center texas\_cities  
near\_sacramento = City.find\_by\_name(‘Sacramento’).nearbys(100)

You can see the gem homepage at [GitHub - alexreisner/geocoder: Complete Ruby geocoding solution.](http://github.com/alexreisner/geocoder)  
Wasn’t very straightforward how to use this outside of Rails, but I  
eventually got it. (had to do a little code digging to understand the  
errors  
and query some mailing lists to find solutions)  
$ gem install rails-geocoder

Then get a google api key at

> **[الأسئلة الشائعة حول &qout;منصة خرائط Google&qout;
  
  | ...](https://developers.google.com/maps/faq?csw=1&hl=ar)**

The license says your application must be available to the public, free  
or  
charge, otherwise you should be using

> **[הפלטפורמה של מפות Google
  
  |  Google Maps Platform  | ...](https://developers.google.com/maps/)**
>
> תיאורים של הפלטפורמה של מפות Google

They want you to list a site your app will be available through. I gave  
them  
the URL of the gist the example is hosted at :)~

Anyway, a very simple program that correctly uses the gem to get  
results:  
require ‘net/http’  
require ‘rubygems’  
require ‘active\_record’  
require ‘geocoder’

Geocoder::GOOGLE\_MAPS\_API\_KEY = ‘your key goes here’  
p Geocoder.fetch\_coordinates(“Wichita,KS,US”)
