Last active
July 13, 2016 15:59
-
-
Save glennfu/4ebe0c7432a1a48bec45567255ff4ede to your computer and use it in GitHub Desktop.
Ways to get data out of geokit calls to Google
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
def parse_stuff | |
geoloc = Geokit::Geocoders::GoogleGeocoder3.geocode address.gsub("\n", ", ") | |
placemark = JSON.parse(geoloc.to_json) | |
# Sometimes the most accurate entry doesn't have a city, but a less accurate version does | |
if city.blank? && region.present? && geoloc.respond_to?(:all) | |
placemark["city"] = geoloc.all.detect(&:city).try(&:city) | |
end | |
end | |
def city | |
placemark && (placemark["City"] || placemark["city"]) | |
end | |
def sublocality | |
return nil unless placemark | |
subloc = placemark["SubLocality"] || placemark["Sublocality"] || placemark["sublocality"] || placemark["sublocality"] | |
if subloc.present? | |
subloc | |
else | |
placemark["district"] | |
end | |
end | |
def region | |
temp_region = raw_region | |
country = country_code || 'US' | |
if 'US' == country && temp_region && state = Location.us_states.detect{|s|s[1] == temp_region} | |
state[0] | |
else | |
temp_region | |
end | |
end | |
def raw_region | |
placemark && (placemark["State"] || placemark["state"]) | |
end | |
def country_code | |
placemark && (placemark["CountryCode"] || placemark["country_code"]) | |
end | |
def zip | |
placemark && (placemark["ZIP"] || placemark["zip"]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment