从Geocode经度和纬度获取地址
对于我的大部分应用程序,我从Geocoder获取城镇/城市的纬度,经度,邮政编码等。 我刚刚进入城市和州,作为回报,我得到了
我在一个场地,我有一个场地。 那个场地需要一个地址,我从另一个来源获得那个场地的纬度和经度。 使用Geocoder gem,我能通过给它一个纬度和经度来获得一个地址吗?
使用reverse_geocode_by
。 一切都在这里: https : //github.com/alexreisner/geocoder
在Rails console
运行。
latitude = 40.0397 longitude = -76.30144 geo_localization = "#{latitude},#{longitude}" query = Geocoder.search(geo_localization).first query.address
如果我没有错,你想从纬度和经度获得地址,这可以使用RubyGeocoder完成。
通过坐标查看简单反向地理编码,其中说:
给定具有已知纬度/经度坐标的Place模型,自动获取地址并存储在location属性中(如果省略地址选项,则存储在地址属性中):
# app/models/place.rb reverse_geocoded_by :latitude, :longitude, address: :location after_validation :reverse_geocode
如果您不想与模型集成:
lat = 40.0397 lon = -76.30144 query = "#{lat},#{lon}" first_result = Geocoder.search(query).first if first_result.present? puts first_result.city end