Created
March 8, 2011 19:05
-
-
Save gmgent/860798 to your computer and use it in GitHub Desktop.
used to get Lat Long values from Yahoo Maps API on the fly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GetLatLong | |
require 'open-uri' | |
attr_reader :latitude | |
attr_reader :longitude | |
attr_reader :message | |
attr_reader :valid | |
attr_writer :address | |
attr_writer :state | |
attr_writer :city | |
attr_writer :zip | |
YAHOO_KEY = "redacted" | |
def initialize | |
@valid=true | |
end | |
def run | |
load_info | |
end | |
def load_info | |
#add plus signs to street and city | |
@address=@address.split(" ").collect {|add| "#{add}+"} | |
@city=@city.split(" ").collect {|ci| "#{ci}+"} | |
#wait a little because YAHOO hates too many subsequent calls TOO QUICKLY | |
sleep 1 | |
xpath = "http://local.yahooapis.com/MapsService/V1/geocode?appid=#{YAHOO_KEY}&street=#{@address}&city=#{@city}&state=#{@state}&zip=#{@zip}" | |
uri = URI.parse(xpath) | |
begin | |
uri.open { |f| | |
puts "Fetched document: #{f.base_uri}" | |
# Save the response body | |
@response = f.read | |
} | |
search = XmlSimple.xml_in(@response) | |
@latitude = search["Result"][0]["Latitude"][0] | |
@longitude = search["Result"][0]["Longitude"][0] | |
puts "Your results:\n" | |
rescue => ex | |
puts "error\n#{ex.message} " | |
@valid=false | |
end | |
"lat:#{@latitude} long:#{@longitude}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment