Question

For the bulk of my application, I am getting the latitude, longitude, postal code, etc of a town/city from Geocoder. I am just putting in the city and state and in return I am getting

I am in a scenario where I have a venue. That venue needs an address and I am getting that venues latitude and longitude from another source. Using the Geocoder gem, am I able to get an address by giving it a latitude and longitude?

Was it helpful?

Solution 2

Use reverse_geocode_by. It's all in here: https://github.com/alexreisner/geocoder

OTHER TIPS

Run in Rails console.

latitude = 40.0397
longitude = -76.30144
geo_localization = "#{latitude},#{longitude}"
query = Geocoder.search(geo_localization).first
query.address

If you're not looking to integrate with a model:

lat = 40.0397
lon = -76.30144
query = "#{lat},#{lon}"
first_result = Geocoder.search(query).first
if first_result.present?
  puts first_result.city
end

If i am not wrong you want to get the address from latitude and longitude, and this can be done using RubyGeocoder .

Look in to Simple Reverse Geocoding by Coordinates which says :

Given a Place model with known latitude/longitude coordinates, automatically fetch address and store in location attribute (stored in address attribute if :address option omitted):

# app/models/place.rb

reverse_geocoded_by :latitude, :longitude, address: :location
after_validation :reverse_geocode
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top