2012-02-22 81 views
3

因此,我製作了這段代碼以獲取GPS位置,日期 和時間的值。然後我會嘗試使它們成爲JSON,並通過Wi-Fi將它們發送到服務器。無論如何,問題是它總是讀取以前的位置值 。它總是讀取 之前值提供的JSON的「網絡」部分的值。 當前位置,如果您運行代碼 ,您可以看到打印的只有兩個括號「{}」。在SL4a中使用Python讀取GPS值不起作用

當我直接從手機運行腳本 時,手機上的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

getLastKnownLocation中列出的時間對我來說似乎非常當前(在腳本運行時間的一秒內)。我也爲{}讀取位置,但它似乎並不是一個真正的問題。如果在運行腳本之前打開GPS,我只能在提供者部分獲取GPS值。它似乎像startLocating實際上並沒有打開GPS,如果它關閉。 – 2012-02-23 16:29:41

+0

我在啓動腳本之前打開GPS,但它仍然無法工作。我設法找到了解決方案。如果您還打開了我認爲毫無意義的移動數據連接,則該腳本正常工作,因爲從理論上講,您應該只與GPS衛星「交談」。或者,也許我弄錯了,GPS衛星與載波通信? – raduq 2012-02-23 19:56:24

回答

0

在調用readLocation之前調用eventWaitFor(「location」)。您也可能想要使用eventPoll,eventClearBuffer或eventWait。

GPS傳感器啓動非常慢,因爲它需要獲取衛星,同步時間,然後根據衛星信號的定時對衛星進行三角測量。此外,因爲您在不指定更新頻率的情況下調用了startLocating,所獲得的默認頻率爲60,000ms或每分鐘一次。