Get address from Geocode latitude and longitude

旧城冷巷雨未停 提交于 2019-12-05 08:31:28

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

Run in Rails console.

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

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

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