3

所以我编写了这段代码来获取 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)
4

1 回答 1

0

在调用 readLocation 之前调用 eventWaitFor("location")。您可能还想使用 eventPoll、eventClearBuffer 或 eventWait。

GPS 传感器的启动速度非常慢,因为它需要获取卫星、同步时间,然后根据来自卫星的信号时间进行三角定位。此外,由于您在没有指定更新频率的情况下调用了 startLocating,因此您将获得 60,000 毫秒或每分钟一次的默认频率。

于 2012-02-25T15:39:30.440 回答