Mar 24 geokit gem: getting country from Google reverse geocoding

tags: geokit geocoding gem | comments

geokit ruby gem provides geocoding and distance/heading calculations. Paired with the geokit-rails plugin you get a full-fledged location-based app with lot’s of goodies for geolocation, ip geolocation, reverse geocoding, distance calculations between models and more.

I’ve been using it for a couple of months now, and it’s really worth it, but still when you reverse_geocode a lat/lng pair per default you get country code (e.g CU) but no country name (e.g Cuba). In a localized app that’s cool ’cos you then map to the country name in the language you want, but I need to get the english name to make my specs and features pass before I get into the i18n and l10n thing.

So I patched geokit to return country name too:

# in lib/geokit/mappable.rb
# I add a new accessor to the GeoLoc class
class GeoLoc

attr_accessor :street_address, :city, :state, :zip, :country_code, :country, :full_address, :all

# and in lib/geokit/mappable.rb, class GoogleGeocoder, method extract_placemark
# I fetch the CountryName from google's xml response

res.country = doc.elements['.//CountryName'].text if doc.elements['.//CountryName']

that’s it, all gem tests still pass.

I have it on my fork of geokit you can grab it from there if you want

blog comments powered by Disqus