Get address from Geocode latitude and longitude

烂漫一生 提交于 2019-12-07 05:09:57

问题


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?


回答1:


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




回答2:


Run in Rails console.

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



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/20827675/get-address-from-geocode-latitude-and-longitude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!