diff --git a/README.rdoc b/README.rdoc index ff4a07bccd9e750e0f61c8e857789eb404bb2ff6..1919355e4b19b22c4a05b357d58a2b93d17e8490 100644 --- a/README.rdoc +++ b/README.rdoc @@ -284,6 +284,7 @@ Quota:: 2,500 requests/day, 100,000 with Google Maps API Premier Region:: world SSL support:: yes Languages:: ar, eu, bg, bn, ca, cs, da, de, el, en, en-AU, en-GB, es, eu, fa, fi, fil, fr, gl, gu, hi, hr, hu, id, it, iw, ja, kn, ko, lt, lv, ml, mr, nl, no, pl, pt, pt-BR, pt-PT, ro, ru, sk, sl, sr, sv, tl, ta, te, th, tr, uk, vi, zh-CN, zh-TW (see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1) +Extra options:: <tt>:bounds</tt> - pass SW and NE coordinates as an array of two arrays to bias results towards a viewport Documentation:: http://code.google.com/apis/maps/documentation/geocoding/#JSON Terms of Service:: http://code.google.com/apis/maps/terms.html#section_10_12 Limitations:: "You must not use or display the Content without a corresponding Google map, unless you are explicitly permitted to do so in the Maps APIs Documentation, or through written permission from Google." "You must not pre-fetch, cache, or store any Content, except that you may store: (i) limited amounts of Content for the purpose of improving the performance of your Maps API Implementation..." diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb index ed7f1cd3aadbbb161c03eca6a0d16242b5d995d4..967294f53c3abfe4728b6710e9121e861069a50f 100644 --- a/lib/geocoder/lookups/google.rb +++ b/lib/geocoder/lookups/google.rb @@ -27,13 +27,22 @@ module Geocoder::Lookup return [] end - def query_url(query) + def query_url_base_params(query) params = { (query.reverse_geocode? ? :latlng : :address) => query.sanitized_text, :sensor => "false", - :language => Geocoder::Configuration.language, - :key => Geocoder::Configuration.api_key + :language => Geocoder::Configuration.language } + unless (bounds = query.options[:bounds]).nil? + params[:bounds] = bounds.map{ |point| "%f,%f" % point }.join('|') + end + params + end + + def query_url(query) + params = query_url_base_params(query).merge( + :key => Geocoder::Configuration.api_key + ).reject{ |key, value| value.nil? } "#{protocol}://maps.googleapis.com/maps/api/geocode/json?" + hash_to_query(params) end end diff --git a/lib/geocoder/lookups/google_premier.rb b/lib/geocoder/lookups/google_premier.rb index 14cbdac8ee911cf268901d6fa78457d71d85ce25..2f1d974c76ccceceac9fdc6f7df6f80dd0193c56 100644 --- a/lib/geocoder/lookups/google_premier.rb +++ b/lib/geocoder/lookups/google_premier.rb @@ -9,13 +9,10 @@ module Geocoder::Lookup private # --------------------------------------------------------------- def query_url(query) - params = { - (query.reverse_geocode? ? :latlng : :address) => query.sanitized_text, - :sensor => 'false', - :language => Geocoder::Configuration.language, + params = query_url_base_params(query).merge( :client => Geocoder::Configuration.api_key[1], :channel => Geocoder::Configuration.api_key[2] - }.reject{ |key, value| value.nil? } + ).reject{ |key, value| value.nil? } path = "/maps/api/geocode/json?#{hash_to_query(params)}" "#{protocol}://maps.googleapis.com#{path}&signature=#{sign(path)}" end diff --git a/test/services_test.rb b/test/services_test.rb index ef701510675f4df140030812dfe73c3e01b78279..2864588dd465dbb903d4af3eed40211dd0fb89e7 100644 --- a/test/services_test.rb +++ b/test/services_test.rb @@ -28,6 +28,14 @@ class ServicesTest < Test::Unit::TestCase result.precision end + def test_google_query_url_contains_bounds + lookup = Geocoder::Lookup::Google.new + url = lookup.send(:query_url, Geocoder::Query.new( + "Some Intersection", + :bounds => [[40.0, -120.0], [39.0, -121.0]] + )) + assert_match /bounds=40.0+%2C-120.0+%7C39.0+%2C-121.0+/, url + end # --- Google Premier ---