module Geokit module Geocoders # Yahoo geocoder implementation. Requires the Geokit::Geocoders::YAHOO variable to # contain a Yahoo API key. Conforms to the interface set by the Geocoder class. class YahooGeocoder < Geocoder private # Template method which does the geocode lookup. def self.do_geocode(address, options = {}) address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address url="http://api.local.yahoo.com/MapsService/V1/geocode?appid=#{Geokit::Geocoders::yahoo}&location=#{Geokit::Inflector::url_escape(address_str)}" res = self.call_geocoder_service(url) return GeoLoc.new if !res.is_a?(Net::HTTPSuccess) xml = res.body doc = REXML::Document.new(xml) logger.debug "Yahoo geocoding. Address: #{address}. Result: #{xml}" if doc.elements['//ResultSet'] res=GeoLoc.new #basic res.lat=doc.elements['//Latitude'].text res.lng=doc.elements['//Longitude'].text res.country_code=doc.elements['//Country'].text res.provider='yahoo' #extended - false if not available res.city=doc.elements['//City'].text if doc.elements['//City'] && doc.elements['//City'].text != nil res.state=doc.elements['//State'].text if doc.elements['//State'] && doc.elements['//State'].text != nil res.zip=doc.elements['//Zip'].text if doc.elements['//Zip'] && doc.elements['//Zip'].text != nil res.street_address=doc.elements['//Address'].text if doc.elements['//Address'] && doc.elements['//Address'].text != nil res.precision=doc.elements['//Result'].attributes['precision'] if doc.elements['//Result'] # set the accuracy as google does (added by Andruby) res.accuracy=%w{unknown country state state city zip zip+4 street address building}.index(res.precision) res.success=true return res else logger.info "Yahoo was unable to geocode address: "+address return GeoLoc.new end rescue logger.info "Caught an error during Yahoo geocoding call: "+$! return GeoLoc.new end end end end