[ 1 ]
def get_coords(address)
coords = Geocoder.search(address)
self.lat = coords[0].data["lat"].to_f
self.lng = coords[0].data["lon"].to_f
end
在前端,用户输入新 TravelCenter、Restaurant、Lodging 或 CoffeeShop 的地址。在 Rails 中,我使用 Geocoder 来获取保存在数据库中的纬度和经度。然后回到前端,我为给定区域中的每个“地点”显示一个标记。
问题是,以这种方式创建新条目时,坐标距离很远。第一个图像显示了 Rails 控制台中最新的 TravelCenter,图像左侧显示了它所代表的实际卡车停靠站的真实世界坐标。请注意,经度向左太远。在前端,应该位于单词 Amarillo 东侧的蓝点位于西侧。
所以基本上......什么给了?这个特定的 API 有什么问题吗?
此外,是否有一种不那么混乱的方法来获取它们的坐标?我不知道为什么 Geocoder.search 返回一个数组以及为什么 lat 和 lng 被埋得这么深。