Skip to content

Instantly share code, notes, and snippets.

@stgeneral
Last active January 4, 2016 11:49
Show Gist options
  • Save stgeneral/8617937 to your computer and use it in GitHub Desktop.
Save stgeneral/8617937 to your computer and use it in GitHub Desktop.
require 'geocoder'
#Example postal code '75003' has several locations and one of them is in Ukraine (not a EU member for now :)
query = '75003'
puts "\n=== Search with Google for '#{query}' ==="
# Specify countries of our interest to exclude irrelevant results
eu_countries = %w(France Estonia)
Geocoder.search(query).each do |point|
puts "#{point.latitude},#{point.longitude} (#{point.address})" if eu_countries.include?(point.country)
end
puts "\n=== Search with Nominatim for '#{query}' ==="
Geocoder.configure(lookup: :nominatim)
# Some lookups has extra params to limit search locations
eu_codes = "fr,ee"
Geocoder.search(query, params: {countrycodes: eu_codes}).each do |point|
puts "#{point.latitude},#{point.longitude} (#{point.address})"
end
query = '47.382762,8.503994'
puts "\n=== Search with Nominatim for '#{query}' ==="
Geocoder.search(query).each do |point|
puts "#{point.postal_code} (#{point.address})"
end
# Output
=begin
=== Search with Google for '75003' ===
48.8634799,2.3591145 (75003 Paris, France)
59.0396196,25.2245383 (75003, Estonia)
48.8638194,2.3616583 (3rd arrondissement of Paris, Paris, France)
=== Search with Nominatim for '75003' ===
48.8644716589675,2.35860892258141 (Arts-et-Métiers, 3rd Arrondissement, Paris, Ile-de-France, 75003, Metropolitan France)
45.7039072,4.9767771 (75003, Rue Victor Schœlcher, Saint-Priest, Lyon, Rhône, Rhône-Alpes, 69800, Metropolitan France)
=== Search with Nominatim for '47.382762,8.503994' ===
8004 (Stadion Letzigrund, Herdernstrasse, Hard, Aussersihl, Zurich, Bezirk Zürich, Zurich, 8004, Switzerland)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment