0
votes

I have zip codes stored in the Users table, and in the Locations table I have lat, lon, and the zips data from all 50 states.

I am trying to finish this line:

:geo => [current_user.latitude * Math::PI / 180.0, current_user.longitude * Math::PI / 180.0]

The issue is the User stores zip code only. How can I define so that current_user.latitude would show the latitude based on the current users zip code?

1
As a for instance, you can use USPS data sets which have information correlating geo references with mail addresses and zip codes. Overall it is not a simple calculation - it is a complex look up.. Postal data set information for mass mailers testing: usps.com/postalone/testing.htm - jim mcnamara
That would be much more than what I am looking to do. I just want to output the latitude and longitude values (stored in Locations table that includes zips with lat/lon) from the current users zip code. I have the relationship between Users and Locations, and the data imported. USPS data sets would not be helpful for this situation. Thanks though! - xps15z
You can use the geocoder api to get the latitude and longitude. But then it would have to be a proper zip code. unlike countries like America , if you enter the zip code as 742 and the country as India, you would get lots of records. - argentum47

1 Answers

0
votes

I'm not entirely clear on the structure of your locations table, but what about something like the following:

class User < ActiveRecord::Base
  def location
    @location ||= Location.where(zip: zipcode)
  end

  def latitude
    location.lat
  end

  def longitude
    location.lon
  end
end

The real issue is how you have to construct the join between your users table and your locations table. If they each have a column which contains a string of the ZIP code, it just requires a simply where condition like the one shown in User#location above.