[
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、餐厅、住宿或CoffeeShop的地址。在Rails中,我使用地理编码器来获取保存在数据库中的纬度和经度。然后回到前端,我为给定区域中的每个“位置”显示一个标记。
问题是,当以这种方式创建新条目时,坐标会出现偏差。第一个图像显示了Rails控制台中的最新TravelCenter,图像左侧显示了它所表示的实际卡车停靠站的真实坐标。请注意,经度太靠左了。在前端,应该在单词Amarillo东侧的蓝点在西侧。
那么basically...what给了?这个特定的API有什么问题吗?
此外,有没有一种不那么麻烦的方法来获取它们的坐标?我不知道为什么Geocoder.search会返回一个数组,为什么lat和lng会被隐藏得如此之深。
更新:左侧,预期位置。对,实际位置。结论: Ruby地理编码器是垃圾。
发布于 2019-12-31 01:35:41
您可以尝试下面的方法,您不必从字符串转换为浮点型请参阅此reference if you need more detail
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
https://stackoverflow.com/questions/59537292
复制相似问题