我有一个错误,指出"列出索引超出范围"。我已经在代码中到处查找可以在两个集合数字范围内调用的数字。如果我走错了路,我深表歉意,因为我对此很陌生。
这是代码:
#!/usr/bin/python
import sys,time,geolocation,publisher
from subprocess import call
SleepTime = 10 # seconds
_lat = 0.00
_lon = 0.00
def maintain():
global _lat
global _lon
(lat,lon,accuracy) = geolocation.getLocation()
if(lat != _lat or lon !=_lon):
data = str(lat) + "," + str(lon) + "," + str(accuracy)
print ("publishing") , data
publisher.publishtoInternet(data)
_lat = lat
_lon = lon
else:
print ("no change in coordinates")
print ("program begins")
while True:
try:
maintain()
except Exception as inst:
print (type)(inst), ('exception captured')
print (inst)
sys.stdout.flush()
#file = open('/tmp/loctracker.error.log','a')
#file.write('exception occured, trying to reboot')
#file.close()
#call(["sudo","reboot"])
#break
for i in range(0,SleepTime):
sys.stdout.write ("restarting in %d seconds " % (SleepTime-i))
sys.stdout.flush()
time.sleep(1)
该程序假设从您的网络IP地址中找到位置并将数据发送到谷歌地图,然后发送回位置,然后将其发送到 Sparkfish.com 以存储数据。
我用下面的答案更改了代码,但仍然没有运气。这是代码和屏幕截图。
#!/usr/bin/python
import sys,time,geolocation,publisher
from subprocess import call
SleepTime = 10 # seconds
_lat = 0.00
_lon = 0.00
def maintain():
global _lat
global _lon
(lat,lon) =geolocation.getLocation()
if(lat != _lat or lon != _lon):
data = str(lat) + "," + str(lon) + "," + str(accuracy)
print ("publishing") , data
publisher.publishtoInternet(data)
_lat = lat
_lon = lon
else:
print ("no change in coordinates")
print ("program begins")
while True:
try:
maintain()
except Exception as inst:
print (type)(inst), ('exception captured')
print (inst)
sys.stdout.flush()
#file = open('/tmp/loctracker.error.log','a')
#file.write('exception occured, trying to reboot')
#file.close()
#call(["sudo","reboot"])
#break
for i in range(0,SleepTime):
sys.stdout.write ("restarting in %d seconds " % (SleepTime-i))
sys.stdout.flush()
time.sleep(1)
屏幕截图 !(https://onedrive.live.com/redir?resid=2BBD197F439CC237!30330&authkey=!AJv8LN5_4gHqKpI&ithint=file%2c)
你可能想好好看看 https://developers.google.com/maps/documentation/geolocation/intro#responses正如它所说,
geolocation.getLocation()
只返回两个对象,即纬度和经度。您正在尝试解压缩 3 个变量元组中的值,这给您带来了错误!尝试
(lat, lon) = geolocation.getLocation()
我不确定您将如何获得准确性以及是否有任何类似的方法
geolocation.getAccuracy()
存在与否,因为我从未使用过该模块。只是从文档中解释它!
可能是从您的(lat,lon,accuracy)= geolocation.getLocation()返回的,请查看下面的源代码。 您的作业与 JSON 格式不匹配。 JSON 格式将 LON 和 lat 作为位置的子字段,并且准确性本身。
地理位置响应
成功的地理位置请求将返回定义位置和半径的 JSON 格式响应。
位置:用户的估计纬度和经度(以度为单位)。包含一个纬度和一个液化天然气子字段。精度:估计位置的精度(以米为单位)。这表示围绕给定位置的圆的半径。{ "位置":{ "纬度": 51.0, "液化天然气":-0.1 }, "精度":1200.4}
https://developers.google.com/maps/documentation/geolocation/intro#wifi_access_point_object