所以我已经制作了这段代码来获取GPS位置,日期的值 和时间。然后我会尝试用JSON制作它们并发送它们 通过Wi-Fi到服务器。无论如何,问题是它总是读取 以前的位置值。它始终从中读取值 “网络”部分由前面的值提供的JSON。该 当前位置,打印只有两个括号“{}”,你可以看到 你运行代码。
运行脚本时,手机上的GPS图标无法激活 直接从手机。我想代码是自我解释但是如果 有任何问题请问。
import android,time,datetime
droid = android.Android()
latitudine = "initial value"
longitudine = "initial value"
indexVal = 1
lostSignal = False
while True:
print "~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~\nNew reading: #" + str(indexVal) +"\n"
droid.startLocating()
locatie = droid.readLocation().result
droid.stopLocating()
print "Reading result: " + str(locatie)
if locatie == {}:
print "Current location information not available"
locatie=droid.getLastKnownLocation().result
if locatie != {}:
if locatie['gps'] == None:
if locatie['network'] == {}:
latitudine = str(locatie['passive']['latitude'])
longitudine = str(locatie['passive']['longitude'])
print "Reading passive data (from last known location):"
print longitudine
print latitudine
lostSignal = True
elif locatie['network'] != {}:
latitudine = str(locatie['network']['latitude'])
longitudine = str(locatie['network']['longitude'])
print "Reading data from network:"
print latitudine
print longitudine
lostSignal = True
elif locatie['gps']!=None:
if lostSignal == True:
droid.vibrate(500)
lostSignal = False
latitudine=str(locatie['gps']['latitude'])
longitudine=str(locatie['gps']['longitude'])
print "Reading data from GPS:"
print latitudine
print longitudine
droid.notify("GPS found","GPS signal found.\nData aquired")
print "\nFull available information:\n"
for locInfo in locatie.iteritems():
print str(locInfo)
print "\n"
now = datetime.datetime.now()
ora = now.hour
minut = now.minute
secunda = now.second
ziua = now.day
luna = now.month
an = now.year
print str(ora)+":"+str(minut)+":"+str(secunda)+" / "+str(ziua)+"-"+str(luna)+"-"+str(an)
indexVal += 1
time.sleep(20)
答案 0 :(得分:0)
在调用readLocation之前调用eventWaitFor(“location”)。您可能还想使用eventPoll,eventClearBuffer或eventWait。
GPS传感器启动非常慢,因为它需要获取卫星,同步时间,然后根据卫星信号的定时对位置进行三角测量。此外,由于您在未指定更新频率的情况下调用了startLocating,因此您将获得默认频率60,000毫秒或每分钟一次。