[英]Rails geocoder gem find city
I am trying to find the city of the user, now I can see the address method working fine in my controller 我正在尝试查找用户所在的城市,现在我可以在控制器中看到address方法正常工作了
def your_location
Geocoder.address(request.remote_ip)
end
But I can't figure out how to find the user city. 但是我不知道如何找到用户城市。
From Geocoder GitHub : 从Geocoder GitHub :
Geocoder adds
location
andsafe_location
methods to the standard Rack::Request object so you can easily look up the location of any HTTP request by IP address.Geocoder在标准的Rack :: Request对象中添加了
location
和safe_location
方法,因此您可以通过IP地址轻松查找任何HTTP请求的位置。 For example, in a Rails controller or a Sinatra app:例如,在Rails控制器或Sinatra应用程序中:
# returns Geocoder::Result object
result = request.location
location
hash example: location
哈希示例:
{
"data"=>{
"ip"=>"213.174.0.254",
"city"=>"Lviv",
"latitude"=>49.8383,
"zip_code"=>"79000",
"longitude"=>24.0232,
"time_zone"=>"Europe/Kiev",
"metro_code"=>0,
"region_code"=>"46",
"region_name"=>"L'vivs'ka Oblast'",
"country_code"=>"UA",
"country_name"=>"Ukraine"
},
"cache_hit"=>nil
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.