GPS位置到时区

问题描述 投票:10回答:5

我想知道我的用户发送请求的当地时间。基本上,有这样的功能是这样的

var localTime = getLocalTime( lat, long );

我不确定lat上的简单划分是否可行,因为大多数国家都没有完美的几何形状。

任何帮助都会很棒。任何语言都被接受。我想避免调用远程API。

gps timezone gps-time
5个回答
4
投票

Google Time Zone API似乎是你所追求的。然而,它没有任何free tier

时区API为地球表面上的位置提供时间偏移数据。请求特定纬度/经度对的时区信息将返回该时区的名称,与UTC的时间偏移以及夏令时偏移。


3
投票

用于计算时区的shapefile是not maintained了。

我今天刚遇到同样的问题,而且我不确定我的答案是多么相关,但我基本上只是写了一个Python函数来做你想要的。你可以在这里找到它。

https://github.com/cstich/gpstotz

编辑:

正如评论中所提到的,我也应该发布代码。该代码基于Eric Muller的时区形状文件,你可以在这里找到 - http://efele.net/maps/tz/world/

编辑2:

事实证明,shapefile对外圈和内圈有一个古老的定义(基本上外圈使用右手规则,而内圈使用左手规则)。在任何情况下,fiona似乎都会照顾这一点,因此我更新了代码。

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'

2
投票

几天前我正在寻找相同的东西,不幸的是我找不到API或简单的功能。正如你所说的那样,国家没有完美的几何形状。您必须创建每个时区区域的表示,并查看您的点所在的位置。我认为这将是一个痛苦,我不知道它是否可以完成。

我发现的唯一一个在这里描述:Determine timezone from latitude/longitude without using web services like Geonames.org。基本上,您需要一个包含时区信息的数据库,并且您正在尝试查看哪个最接近您的兴趣点。

但是,我一直在寻找静态解决方案(不使用互联网),因此如果您可以使用互联网连接,您可以使用:http://www.earthtools.org/webservices.htm,它提供了一个web服务,为您提供给定纬度/经度坐标的时区。


0
投票

截至2019年,Google API没有任何免费套餐,@ cstich的数据来源不再维护。

如果您需要API,timezonedb.com提供的免费等级费率仅限1请求/秒。

@cstich使用的数据的原始维护者链接到this project,它从OpenStreetMap检索数据。自述文件包含以各种语言查找库的链接。


-4
投票

难道你不能简单地使用用户IP来确定他们住在哪里?然后使用(Countries |与GMT的差异)数组来获取当地时间。

© www.soinside.com 2019 - 2024. All rights reserved.