diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index 74281f1cf80f4885045b1e357451624e87cef6f2..3b449c454f5cb2d5c8cc033a9cc7bc5e36b22a99 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -20,7 +20,8 @@ module Geocoder # for reverse geocoding. # def search(*args) - if res = result(args.join(","), args.size == 2) + reverse = (args.size == 2) || coordinates?(args.first) + if res = result(args.join(","), reverse) result_class.new(res) end end @@ -116,6 +117,13 @@ module Geocoder !!(ip == "0.0.0.0" or ip.match(/^127/)) end + ## + # Does the given string look like latitude/longitude coordinates? + # + def coordinates?(value) + !!value.to_s.match(/^[0-9\.\-]+, ?[0-9\.\-]+$/) + end + ## # Simulate ActiveSupport's Object#to_query. # Removes any keys with nil value. diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index 782806ef84b4d341658ca6fe153d4c5a09699706..925efb90c5e6747285a480a090ffc1d4fbe2c9fc 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -256,6 +256,13 @@ class GeocoderTest < Test::Unit::TestCase assert_match "appid=MY_KEY", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States") end + def test_detection_of_coordinates_in_search_string + Geocoder::Configuration.lookup = :geocoder_ca + result = Geocoder.search("51.178844, -1.826189") + assert_not_nil result.city + # city only present if reverse geocoding search performed + end + private # ------------------------------------------------------------------