From 6706ca3c5020577c22f69a6590dd6ff4e065c8a4 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 8 Aug 2007 12:37:16 +0000 Subject: [PATCH] Use IP GeoLocation to choose a default view for the slippy map if we don't have any better ideas. --- app/views/site/index.rhtml | 9 +++++++++ lib/osm.rb | 25 ++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/app/views/site/index.rhtml b/app/views/site/index.rhtml index dc488089d..5488ba40d 100644 --- a/app/views/site/index.rhtml +++ b/app/views/site/index.rhtml @@ -33,11 +33,20 @@ <% lat = @user.home_lat %> <% zoom = '10' %> <% else %> +<% session[:location] = OSM::IPLocation(request.env['REMOTE_ADDR']) unless session[:location] %> +<% if session[:location] %> +<% bbox = true %> +<% minlon = session[:location][:minlon] %> +<% minlat = session[:location][:minlat] %> +<% maxlon = session[:location][:maxlon] %> +<% maxlat = session[:location][:maxlat] %> +<% else %> <% lon = '-0.1' %> <% lat = '51.5' %> <% zoom = params['zoom'] || '5' %> <% layers = params['layers'] %> <% end %> +<% end %> <%= javascript_include_tag 'map.js' %> diff --git a/lib/osm.rb b/lib/osm.rb index 77b07ce3d..3d53b453a 100644 --- a/lib/osm.rb +++ b/lib/osm.rb @@ -5,7 +5,7 @@ module OSM # # This would print every latitude value: # - # gpx = OSM:GPXImporter.new('somefile.gpx') + # gpx = OSM::GPXImporter.new('somefile.gpx') # gpx.points {|p| puts p['latitude']} require 'time' @@ -364,4 +364,27 @@ module OSM return doc end end + + def self.IPLocation(ip_address) + Timeout::timeout(4) do + Net::HTTP.start('api.hostip.info') do |http| + country = http.get("/country.php?ip=#{ip_address}").body + country = "GB" if country = "UK" + Net::HTTP.start('ws.geonames.org') do |http| + xml = REXML::Document.new(http.get("/countryInfo?country=#{country}").body) + xml.elements.each("geonames/country") do |ele| + minlon = ele.get_text("bBoxWest").to_s + minlat = ele.get_text("bBoxSouth").to_s + maxlon = ele.get_text("bBoxEast").to_s + maxlat = ele.get_text("bBoxNorth").to_s + return { :minlon => minlon, :minlat => minlat, :maxlon => maxlon, :maxlat => maxlat } + end + end + end + end + + return nil + rescue + return nil + end end -- 2.43.2