简体   繁体   中英

Location with QPython in Android doesn't return GPS coordinates - only "network"

I am writing a very simple script using QPython on my android phone. I would like to extract the GPS coordinates of my current location. However, the script only outputs the "network" coordinates, never the GPS. Here is the code snippet:

#qpy:2
#qpy:console

import androidhelper as android
import time, urllib2
while 1:
    time.sleep(5)
    droid=android.Android()
    droid.startLocating()
    event=droid.eventWaitFor('location', 10000).result
    try:
        lng=event['data']['gps']['longitude']
        lat = event['data']['gps']['latitude']
        print "Longitude: %s ||| Latitude: %s" %(lng,lat) 
    except:
        print "Network Coordinates"

I experimented with the minimum update time eg: droid.startLocating(10000, 0) increased the waiting time for the eventWaitFor() , moved around but the only coordinates I ever get are 'network'. Is there a way to "force" the return of GPS coordinates?

在玩了这个之后,事实证明,如果你调用droid.getLastKnownLocation()并且你移动了一下,你会得到更新的 GPS 坐标

I have an older Nexus 7 tablet where the majority of the results are "network", but "gps" occasionally comes through. On a newer phone, I get a vast majority of "gps" results and at a rate of about 1 every few seconds. The following code filters out the "network" results and also fixes the problem where the lat/lon coordinates are not updated by the system.

   #-*-coding:utf8;-*-
   #qpy:3
   #qpy:console

   import android
   droid = android.Android()

   while True:
       droid.startLocating()
       droid.eventWaitFor('location', int(9000))
       location = droid.readLocation().result
       if len(location) > 0:
           # print('\n', location)
           if 'gps' in location:
               print('\n' + location['gps']['provider'])
               print('Time:' + str(location['gps']['time']))
               print('Lat:' + str(location['gps']['latitude']))
               print('Lon:' + str(location['gps']['longitude']))    
       droid.stopLocating()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM