views:

748

answers:

3

Has anyone done this? I'm confused at how I can make this work, first off I have my user model

Geocoding with it works fine in IRB, just can't figure out how to get it to work in my project.

Trying to use some examples from the readme here: http://github.com/andre/geokit-rails/tree/master.

Anyhow here is what I have:

class User < ActiveRecord::Base

  # geokit
  acts_as_mappable

  after_save :locate

  def locate
    location = Geokit::Geocoders::MultiGeocoder.geocode("12.12.12.12")
  end

end

This corresponds with my save action in my userController, I need to do this after save because authlogic provides the IP after it saves the user or session. I think eventually I'll make it a background process, but till then how can I get this to work? I have a location column in the user model that I'll store the results of geocode()

Also right now I just have some arbitrary IP address "12.12.12.12" but it should actually be current_login_ip

+1  A: 

Haven't used geokit myself so can't comment. But thought that I should mention that HTML 5 supporting browsers (e.g. Firefox 3.5) support the geolocation API in case you weren't aware.

timbo
that’s a great tip, I'm considering using the client at some point and just doing a POST with ajax once the browser locates the user. But until that day, I still need to use the server to geocode the IP address. Sigh... the slow pace of browser acceptance (err... IE unacceptance)
Joseph Silvashy
it would be interesting to compare the two different approaches and see for Firefox, Safari, Chrome, what degree of difference there is between the two methods...
timbo
+1  A: 

Check out this site:

http://geokit.rubyforge.org/readme.html

Scroll down to the IP Geocoding and IP Geocoding Helper sections.

"You can obtain the location for an IP at any time using the geocoder as in the following example:"

location = IpGeocoder.geocode('12.215.42.19')

"where Location is a GeoLoc instance containing the latitude, longitude, city, state, and country code. Also, the success value is true."

Once you get your GeoLoc, just pull your User model, set its long/lat columns and save it.

GeoLoc doc: http://geokit.rubyforge.org/api/geokit-gem/Geokit/GeoLoc.html

Am I missing something?

cakeforcerberus
well, yah you got it spot on, the issue is that I have GeoKit working well, I am just having a hard time figuring out how to save it to my user model correctly after the user logs in.
Joseph Silvashy
+3  A: 
Sean McCleary
awesome! I'm trying to get this to work right now, but this helps a lot, specifically the if loc.success block is helping tremendously.
Joseph Silvashy