user2184718
user2184718

Reputation: 715

Get address from Geocode latitude and longitude

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?

Upvotes: 6

Views: 12243

Answers (4)

Amit Gupta
Amit Gupta

Reputation: 51

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

Upvotes: 3

rplaurindo
rplaurindo

Reputation: 1404

Run in Rails console.

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

Upvotes: 16

mikeorr85
mikeorr85

Reputation: 470

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

Upvotes: 3

Bart
Bart

Reputation: 2656

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

Upvotes: 3

Related Questions