lib/geokit/geocoders/google.rb in geokit-1.8.2 vs lib/geokit/geocoders/google.rb in geokit-1.8.3

- old
+ new

@@ -2,22 +2,20 @@ module Geocoders class GoogleGeocoder < Geocoder config :client_id, :cryptographic_key, :channel private - # Template method which does the reverse-geocode lookup. - def self.do_reverse_geocode(latlng) + # ==== OPTIONS + # * :language - See: https://developers.google.com/maps/documentation/geocoding + def self.do_reverse_geocode(latlng, options = {}) latlng=LatLng.normalize(latlng) - url = submit_url("/maps/api/geocode/json?sensor=false&latlng=#{Geokit::Inflector::url_escape(latlng.ll)}") + url = submit_url("latlng=#{Geokit::Inflector::url_escape(latlng.ll)}", options) process :json, url end - # Template method which does the geocode lookup. - # - # Supports viewport/country code biasing - # # ==== OPTIONS + # * :language - See: https://developers.google.com/maps/documentation/geocoding # * :bias - This option makes the Google Geocoder return results biased to a particular # country or viewport. Country code biasing is achieved by passing the ccTLD # ('uk' for .co.uk, for example) as a :bias value. For a list of ccTLD's, # look here: http://en.wikipedia.org/wiki/CcTLD. By default, the geocoder # will be biased to results within the US (ccTLD .com). @@ -36,17 +34,13 @@ # # When biased to an bounding box around California, it will now return the Winnetka neighbourhood, CA # bounds = Geokit::Bounds.normalize([34.074081, -118.694401], [34.321129, -118.399487]) # Geokit::Geocoders::GoogleGeocoder.geocode('Winnetka', :bias => bounds).state # => 'CA' def self.do_geocode(address, options = {}) bias_str = options[:bias] ? construct_bias_string_from_options(options[:bias]) : '' - language_str = options[:language] ? "&language=#{options[:language]}" : '' address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address - url = submit_url("/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(address_str)}#{bias_str}#{language_str}") - - res = call_geocoder_service(url) - return GeoLoc.new if !res.is_a?(Net::HTTPSuccess) - parse :json, res.body + url = submit_url("address=#{Geokit::Inflector::url_escape(address_str)}#{bias_str}", options) + process :json, url end # This code comes from Googles Examples # http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/urlsigner.rb def self.sign_gmap_bus_api_url(urlToSign, google_cryptographic_key) @@ -59,10 +53,12 @@ # encode the signature into base64 for url use form. Base64.encode64(rawSignature).tr('+/','-_').gsub(/\n/, '') end - def self.submit_url(query_string) + def self.submit_url(query_string, options = {}) + language_str = options[:language] ? "&language=#{options[:language]}" : '' + query_string = "/maps/api/geocode/json?sensor=false&#{query_string}#{language_str}" if client_id && cryptographic_key channel_string = channel ? "&channel=#{channel}" : '' urlToSign = query_string + "&client=#{client_id}" + channel_string signature = sign_gmap_bus_api_url(urlToSign, cryptographic_key) "http://maps.googleapis.com" + urlToSign + "&signature=#{signature}"