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

Fix: handle coordinates string properly.

Get rid of space after comma, if any (Google was returning no results).
parent 6825c3d9
Branches
Tags
No related merge requests found
...@@ -21,7 +21,11 @@ module Geocoder ...@@ -21,7 +21,11 @@ module Geocoder
# for reverse geocoding. Returns an array of <tt>Geocoder::Result</tt>s. # for reverse geocoding. Returns an array of <tt>Geocoder::Result</tt>s.
# #
def search(*args) def search(*args)
reverse = (args.size == 2) || coordinates?(args.first) # if coordinates given as string, split into floats
if coordinates?(args.first)
args = args.first.split(/\s*,\s*/).map{ |i| i.to_f }
end
reverse = (args.size == 2)
results(args.join(","), reverse).map{ |r| result_class.new(r) } results(args.join(","), reverse).map{ |r| result_class.new(r) }
end end
...@@ -120,7 +124,7 @@ module Geocoder ...@@ -120,7 +124,7 @@ module Geocoder
# Does the given string look like latitude/longitude coordinates? # Does the given string look like latitude/longitude coordinates?
# #
def coordinates?(value) def coordinates?(value)
!!value.to_s.match(/^[0-9\.\-]+, ?[0-9\.\-]+$/) !!value.to_s.match(/^[0-9\.\-]+, *[0-9\.\-]+$/)
end end
## ##
......
...@@ -258,6 +258,13 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -258,6 +258,13 @@ class GeocoderTest < Test::Unit::TestCase
assert !Geocoder.send(:blank_query?, "Москва") # no ASCII characters assert !Geocoder.send(:blank_query?, "Москва") # no ASCII characters
end end
def test_coordinates_detection
lookup = Geocoder::Lookup::Google.new
assert lookup.send(:coordinates?, "51.178844,5")
assert lookup.send(:coordinates?, "51.178844, -1.826189")
assert !lookup.send(:coordinates?, "232.65.123")
end
def test_does_not_choke_on_nil_address def test_does_not_choke_on_nil_address
all_lookups.each do |l| all_lookups.each do |l|
Geocoder::Configuration.lookup = l Geocoder::Configuration.lookup = l
...@@ -351,13 +358,6 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -351,13 +358,6 @@ 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").first
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