Rails Get timezone from latitude and longitude using Timezone

馋奶兔 提交于 2019-12-11 05:47:43

问题


Iam working on a Rails app where I want to get timezone from latitude and longitude. I searched for this on stackoverflow and found this link as the best way to do this.

I installed both the gems geokit and timezone and tried to implement the same in my console:

require 'geokit'
require 'timezone'
res = Geokit::Geocoders::GoogleGeocoder.geocode('140 Market St, San Francisco, CA')
timezone = Timezone::Zone.new(:latlon => res.ll)

But I get this error:

ArgumentError: wrong number of arguments (1 for 2)

I checked the documentation for Timezone. They ask to get a username by signing up. But I have not received any activation mail yet. Can someone please suggest some solution for this.

Thanks in advance.


回答1:


It looks like the call Timezone::Zone.new(:latlon => res.ll) does not work (at least I got the same error). I think it is due to changes in format of res.ll value returned from Google. But this call should do the job:

Timezone::Zone.new(lat: res.lat, lon: res.lng)

However, first make sure that you create an account with GeoNames and active it here. You also have to create and configure an initializer for timezone gem. The later is described over here: timezone/lib/timezone/configure.rb



来源:https://stackoverflow.com/questions/12578695/rails-get-timezone-from-latitude-and-longitude-using-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!