GPS定位到时区
我想知道我的用户发送请求的本地时间。 基本上,有这样一个函数吗GPS定位到时区,gps,timezone,gps-time,Gps,Timezone,Gps Time,我想知道我的用户发送请求的本地时间。 基本上,有这样一个函数吗 var localTime = getLocalTime( lat, long ); 我不确定在lat上进行简单的划分是否可行,因为大多数国家都没有完美的几何形状 任何帮助都会很好。任何语言都可以接受。我希望避免调用远程API。您不能简单地使用用户IP来确定他们居住在哪个位置吗?然后你使用一个数组(国家/地区与格林尼治标准时间之差)来获取当地时间。几天前我在搜索同样的东西,不幸的是,我找不到一个API或一个简单的函数来实现它。原因
var localTime = getLocalTime( lat, long );
我不确定在lat上进行简单的划分是否可行,因为大多数国家都没有完美的几何形状
任何帮助都会很好。任何语言都可以接受。我希望避免调用远程API。您不能简单地使用用户IP来确定他们居住在哪个位置吗?然后你使用一个数组(国家/地区与格林尼治标准时间之差)来获取当地时间。几天前我在搜索同样的东西,不幸的是,我找不到一个API或一个简单的函数来实现它。原因正如你所说,各国没有完美的几何形状。您必须创建每个时区区域的表示,并查看点所在的位置。我认为这将是一种痛苦,我不知道这是否可以做到 我找到的唯一一个在这里描述:。基本上,您需要一个包含时区信息的数据库,并且您正在尝试查看哪个时区最接近您的兴趣点 但是,我一直在寻找静态解决方案(不使用internet),因此,如果您可以使用internet连接,那么您可以使用:它提供一个Web服务,为您提供给定纬度/经度坐标的时区。这似乎就是您要找的。然而,它没有任何新功能 时区API提供地球表面位置的时间偏移数据。请求特定纬度/经度对的时区信息将返回该时区的名称、与UTC的时间偏移量以及夏令时偏移量
用于计算时区的shapefile不再是了。 我今天也遇到了同样的问题,我不确定我的答案到底有多重要,但我基本上只是编写了一个Python函数,它可以满足您的需要。你可以在这里找到它 编辑: 正如在评论中提到的,我也应该发布代码。代码基于Eric Muller的时区形状文件,您可以在这里看到- 编辑2: 事实证明,Shapefile对外环和内环的定义有些过时(基本上,外环使用右手规则,而内环使用左手规则)。在任何情况下,菲奥娜似乎都会处理这个问题,我也相应地更新了代码。
from rtree import index # requires libspatialindex-c3.deb
from shapely.geometry import Polygon
from shapely.geometry import Point
import os
import fiona
''' Read the world timezone shapefile '''
tzshpFN = os.path.join(os.path.dirname(__file__),
'resources/world/tz_world.shp')
''' Build the geo-index '''
idx = index.Index()
with fiona.open(tzshpFN) as shapes:
for i, shape in enumerate(shapes):
assert shape['geometry']['type'] == 'Polygon'
exterior = shape['geometry']['coordinates'][0]
interior = shape['geometry']['coordinates'][1:]
record = shape['properties']['TZID']
poly = Polygon(exterior, interior)
idx.insert(i, poly.bounds, obj=(i, record, poly))
def gpsToTimezone(lat, lon):
'''
For a pair of lat, lon coordiantes returns the appropriate timezone info.
If a point is on a timezone boundary, then this point is not within the
timezone as it is on the boundary. Does not deal with maritime points.
For a discussion of those see here:
http://efele.net/maps/tz/world/
@lat: latitude
@lon: longitude
@return: Timezone info string
'''
query = [n.object for n in idx.intersection((lon, lat, lon, lat),
objects=True)]
queryPoint = Point(lon, lat)
result = [q[1] for q in query
if q[2].contains(queryPoint)]
if len(result) > 0:
return result[0]
else:
return None
if __name__ == "__main__":
''' Tests '''
assert gpsToTimezone(0, 0) is None # In the ocean somewhere
assert gpsToTimezone(51.50, 0.12) == 'Europe/London'
截至2019年,谷歌API没有任何免费层,@cstich answer的数据源不再维护 如果您想要一个API,提供一个免费的层速率限制为每秒1个请求
@cstich链接使用的数据的原始维护者,用于从OpenStreetMap检索数据。自述文件包含以多种语言查找库的链接 好的,它涉及到一个远程API,但是看看这个问题的答案。它可能会给你想要的:。你应该在这里发布相关代码,而不是要求人们离开现场。其他网站的链接可能会过期,SO搜索功能无法搜索。这很有意义,只需发布代码即可。有没有办法将文件附加到答案上?