0

[ 预期坐标与实际坐标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 被埋得这么深。

更新:左,预期位置。对,实际位置。结论:Ruby Geocoder 是垃圾。 实际与预期

4

1 回答 1

0

您可以在下面尝试此方法,如果您需要更多详细信息,则不必从字符串转换为浮点数,请参阅此参考

def get_coords(address)
    coords = Geocoder.search(address)
    # your result is in array coords.first.coordinates
    # first index is your lat and second is lng
    self.lat = coords.first.coordinates[0]
    self.lng = coords.first.coordinates[1]
end
于 2019-12-31T01:35:40.610 回答