]> git.openstreetmap.org Git - rails.git/commitdiff
Use IP GeoLocation to choose a default view for the slippy map if
authorTom Hughes <tom@compton.nu>
Wed, 8 Aug 2007 12:37:16 +0000 (12:37 +0000)
committerTom Hughes <tom@compton.nu>
Wed, 8 Aug 2007 12:37:16 +0000 (12:37 +0000)
we don't have any better ideas.

app/views/site/index.rhtml
lib/osm.rb

index dc488089dc1c7d6b5390ba27a5d261b37389dfcc..5488ba40d5cd85d233dde442e5c31cfc3f9dc35d 100644 (file)
 <% lat =  @user.home_lat %>
 <% zoom = '10' %>
 <% else %>
 <% 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 %>
 <% lon =  '-0.1' %>
 <% lat =  '51.5' %>
 <% zoom =  params['zoom'] || '5' %>
 <% layers = params['layers'] %>
 <% end %>
+<% end %>
 
 <script type="text/javascript" src="/openlayers/OpenLayers.js"></script>
 <%= javascript_include_tag 'map.js' %>
 
 <script type="text/javascript" src="/openlayers/OpenLayers.js"></script>
 <%= javascript_include_tag 'map.js' %>
index 77b07ce3d53bd2b038bdae1eb736916b0e4c0c80..3d53b453a8b41f2e164dbdc42b36a8755045cb2c 100644 (file)
@@ -5,7 +5,7 @@ module OSM
   #
   # This would print every latitude value:
   #
   #
   # 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'
   # gpx.points {|p| puts p['latitude']}
 
   require 'time'
@@ -364,4 +364,27 @@ module OSM
       return doc
     end
   end
       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
 end