[英]Rails geocoder gem geocoding using a custom address format
I am using geocoder to get latitude and longitude from address 我正在使用Geocoder从地址获取经度和纬度
In model I added 在模型中我添加了
class Clinic < ApplicationRecord
geocoded_by :full_street_address
after_validation :geocode
def full_street_address
[address_line1, city, state, country].compact.join(', ')
end
end
and client has provided me addresses like this 客户提供了我这样的地址
{
"name": "Bahrain Defence Force Royal Medical Services Hospital - Riffa",
"address_line1": "West Riffa P O Box 28743",
"country": "Bahrain",
"state": "Al-Wusta",
"city": "Riffa"
}
Now when I create a clinic its not returning latitude and longitude, 现在,当我创建一个诊所时,它不会返回经纬度,
Seems that issue is with address and than with following line 似乎问题出在地址,而不是下一行
[address_line1, city, state, country].compact.join(', ')
[address_line1,城市,州,国家/地区] .compact.join(',')
Is there a way I can specifically provide zipcode etc or how to handle above address? 有没有一种方法可以专门提供邮政编码等,或者如何处理上述地址?
Assuming that you followed the Geocoder documentation and added the longitude
and latitude
attributes to your model, I tried your code, you just need to add after_validation :geocode
or use the method :geocode
in any callback you want. 假设您遵循了Geocoder文档,并在模型中添加了
longitude
和latitude
属性,则尝试了代码,您只需要在需要的任何回调中添加after_validation :geocode
或使用方法:geocode
。
EDIT: 编辑:
Okay so I think I didn't refresh the page by the time your question was updated, but I tried the exact same code and it worked as expected. 好的,我想在您的问题更新时我没有刷新页面,但是我尝试了完全相同的代码,并且按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.