Skip to content
Snippets Groups Projects
Commit 63dd486a authored by Alex Reisner's avatar Alex Reisner
Browse files

Detect when search string is actually coordinates.

parent 2f1729e7
Branches
Tags
No related merge requests found
...@@ -20,7 +20,8 @@ module Geocoder ...@@ -20,7 +20,8 @@ module Geocoder
# for reverse geocoding. # for reverse geocoding.
# #
def search(*args) 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) result_class.new(res)
end end
end end
...@@ -116,6 +117,13 @@ module Geocoder ...@@ -116,6 +117,13 @@ module Geocoder
!!(ip == "0.0.0.0" or ip.match(/^127/)) !!(ip == "0.0.0.0" or ip.match(/^127/))
end 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. # Simulate ActiveSupport's Object#to_query.
# Removes any keys with nil value. # Removes any keys with nil value.
......
...@@ -256,6 +256,13 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -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") assert_match "appid=MY_KEY", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States")
end 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 # ------------------------------------------------------------------ private # ------------------------------------------------------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment