我收到错误消息,指出“列表索引超出范围”。我已经在代码中的任何地方查找了可以在两个设定数字的范围内调用数字的位置。如果我在错误的路线上,我很抱歉,因为我对此很陌生。
以下是代码:
#!/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)
答案 0 :(得分:1)
你可能想要正确看一下https://developers.google.com/maps/documentation/geolocation/intro#responses 正如它所说,
geolocation.getLocation()
仅返回两个对象,即纬度和经度。 你试图解压缩3变量元组中的值,这会给你错误! 尝试
(lat, lon) = geolocation.getLocation()
我不确定你将如何获得准确性以及是否有任何方法如
geolocation.getAccuracy()
是否存在因为我从未使用过模块。只是从文档中解释它!
答案 1 :(得分:1)
可能从您的(lat,lon,accuracy)= geolocation.getLocation()返回错误,请查看下面的源代码。你的作业与json格式不匹配。 json格式化具有lon和lat作为位置的子场,并且它具有准确性。
地理位置响应
成功的地理定位请求将返回定义位置和半径的JSON格式的响应。
位置:用户的估计纬度和经度,以度为单位。包含一个lat和一个lng子字段。 准确度:估计位置的准确度,以米为单位。这表示给定位置周围的圆的半径。 { " location":{ " lat":51.0, " lng":-0.1 }, "准确度":1200.4 }
https://developers.google.com/maps/documentation/geolocation/intro#wifi_access_point_object