我正在尝试使用Google Latitude API的 v1(通过 OAuth 2.0)检索我的位置历史记录,特别是list方法。
当尝试检索两个时间戳之间的记录时,我没有得到任何位置。但是,我可以检索我最近的位置(约 860 个,仅在过去 24 小时内)。尝试在与“最近”集中返回的位置相同的时间段内检索位置也会返回零结果。
有谁知道我在这里做错了什么,或者问题在于纬度?我可以通过这种方式检索我的位置记录吗?
已解决:指定的开始和结束时间戳需要以毫秒为单位
以下是我用来帮助将datetime
对象转换为毫秒的一些函数:
def dt_from_epoch(epoch):
epoch = int(epoch)
secs = epoch/1000
mils = epoch - (secs*1000)
dt = datetime.datetime.fromtimestamp(secs)
dt.replace(microsecond=mils*1000)
return dt
def dt_to_mils(dt):
return int((time.mktime(dt.timetuple())*1000) + (dt.microsecond/1000))
原始示例
测试用例脚本:
# Service obtained in the usual OAuth 2.0 way using oauth2client and
# apiclient.discovery.build.
# Scope used: https://www.googleapis.com/auth/latitude.all.best
print "## Retrieve most recent history"
locs = service.location().list(granularity='best', max_results=1000).execute()
first = locs['items'][len(locs['items'])-1]
last= locs['items'][0]
first = datetime.datetime.fromtimestamp(int(first['timestampMs'][:-3]))
last = datetime.datetime.fromtimestamp(int(last['timestampMs'][:-3]))
print "%s - %s: %d" % (first.strftime('%Y-%m-%d %H:%M:%S'),
last.strftime('%Y-%m-%d %H:%M:%S'), len(locs['items']))
print "## Retrieve history between %s and %s" % (
first.strftime('%Y-%m-%d %H:%M:%S'), last.strftime('%Y-%m-%d %H:%M:%S'))
locs = service.location().list(
granularity='best',
min_time=int(time.mktime(first.timetuple())),
max_time=int(time.mktime(last.timetuple())),
max_results=1000
).execute()
results = len(locs['items']) if 'items' in locs else 0
print "%s - %s: %d" % (first.strftime('%Y-%m-%d %H:%M:%S'),
last.strftime('%Y-%m-%d %H:%M:%S'), results)
脚本输出:
## Retrieve most recent history
2012-08-25 23:02:12 - 2012-08-26 15:37:09: 846
## Retrieve history between 2012-08-25 23:02:12 and 2012-08-26 15:37:09
2012-08-25 23:02:12 - 2012-08-26 15:37:09: 0
“最近历史”的 JSON 输出:
{
"data": {
"kind": "latitude#locationFeed",
"items": [
{
"kind": "latitude#location",
"timestampMs": "1345995443316",
"latitude": (latitude),
"longitude": (longitude)
},
(continues...)
]
}
}
“X 和 Y 之间的历史”的 JSON 输出:
{
"data": {
"kind": "latitude#locationFeed"
}
}