繁体   English   中英

Rails TZInfo :: Timezone帮助

[英]Rails TZInfo::Timezone help

我使用Rails TZInfo::Timezone

我该如何在其中添加新城市。

例如:我想向我的应用程序显示“华盛顿特区”时区,但该城市没有可用的地图。

我使用TZInfo::Timezone.get()方法检索所有时TZInfo::Timezone.get()

请给一些专家建议。

这有点涉及,但是您可以使用geocoder获取城市的纬度和经度,然后使用geocoders从geonames.org获取时区。 这是Ruby中的示例:

search_location = 'Washington, DC'

geocoder_results = Geocoder.search(search_location)
# This assumes the city name will always get results from geocoder
nearest_lat = geocoder_results[0].latitude
nearest_lon = geocoder_results[0].longitude

resp = Net::HTTP.get_response(URI.parse('http://api.geonames.org/timezoneJSON?lat=' + nearest_lat.to_s + '&lng=' + nearest_lon.to_s + '&username=your_user_name_here'))
geonames_tz_info = JSON.parse(resp.body)
city_time_zone = geonames_tz_info['timezoneId']

time_zone = TZInfo::Timezone.get(city_time_zone)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM