首页 > 解决方案 > Ruby Geocoder 和/或 OpenStreetMap 存在很大的误差

问题描述

我在 Rails 中使用地理编码器 gem 来获取地址的纬度和经度,然后在 OpenStreetMap 中显示它们。

当我搜索我的旧地址时:

>> results = Geocoder.search("1000 Mount Curve Ave E, Altadena, CA 91001")

我得到:

>> results.first.coordinates
=> [34.1976645, -118.1278219]

安装曲线地址差异

那些坐标可能在一千英尺之外。(见图。)从谷歌地图得到的准确坐标是 [34.200503,-118.1310407]。

我试过另一个地址,它离得更远,可能有一英里。(1346 E Woodbury Rd,帕萨迪纳,CA 91104)

我已经尝试了另一个地址,它非常准确。(922 E Brockton Ave,雷德兰兹,CA 92374)

有谁知道可能导致这些不准确的原因以及如何始终如一地获得准确的结果?

标签: openstreetmaprails-geocoder

解决方案


由于 OSM/Nominatim 的不准确和/或限制,我切换到谷歌地图服务。在我的控制器顶部,我添加了:

require 'google_maps_service'

在我的控制器的显示例程中,我最终得到了这个,它产生了准确的结果:

source = Property.find(params[:id])
@property = PropertyDecorator.new(source)
gmaps = GoogleMapsService::Client.new(key: '[removed, put in your own key here]')
results = gmaps.geocode("#{@property.address1} #{@property.address2}")
if results[0] == nil
  @lat = 0 
  @lng = 0 
else
  @lat = results[0][:geometry][:location][:lat]
  @lng = results[0][:geometry][:location][:lng]
end

推荐阅读