Get precise GPS location in QPython on Android

2,189 views
Skip to first unread message

Tobias Müller

unread,
Apr 22, 2015, 9:15:37 AM4/22/15
to python-fo...@googlegroups.com
Hey everyone, 

I got a script running on my phone, (in QPython3) that fetches location data from the phone and sends it to a socket.
But the data is really not precise at all, and I did some research. 

Android has 3 types of LocationProviders, 

  • gps: pure gps location, slow, energy consuming, but very accurate, and exactly what I need.
  • network: mix of gps and wifi/cell locating, faster, but less accurate
  • passive: like above but completely without using gps
My script automatically uses "network" as LocationProvider, wich is not accurate enough.

Question:
So, how do I select "gps" as location provider, or how do I disable "network" and "passive"?

Code:
# import needed modules import android import time import sys, select, os #for loop exit #Initiate android-module droid = android.Android() #notify me droid.makeToast("fetching GPS data") print("start gps-sensor...") droid.startLocating() while True: #exit loop hook if sys.stdin in select.select([sys.stdin], [], [], 0)[0]: line = input() print("exit endless loop...") break #wait for location-event event = droid.eventWaitFor('location',10000).result if event['name'] == "location": try: #try to get gps location data timestamp = repr(event['data']['gps']['time']) longitude = repr(event['data']['gps']['longitude']) latitude = repr(event['data']['gps']['latitude']) altitude = repr(event['data']['gps']['altitude']) speed = repr(event['data']['gps']['speed']) accuracy = repr(event['data']['gps']['accuracy']) loctype = "gps" except KeyError: #if no gps data, get the network location instead (inaccurate) timestamp = repr(event['data']['network']['time']) longitude = repr(event['data']['network']['longitude']) latitude = repr(event['data']['network']['latitude']) altitude = repr(event['data']['network']['altitude']) speed = repr(event['data']['network']['speed']) accuracy = repr(event['data']['network']['accuracy']) loctype = "net" data = loctype + ";" + timestamp + ";" + longitude + ";" + latitude + ";" + altitude + ";" + speed + ";" + accuracy print(data) #logging time.sleep(5) #wait for 5 seconds print("stop gps-sensor...") droid.stopLocating()

Tobias Müller

unread,
Apr 22, 2015, 9:18:50 AM4/22/15
to python-fo...@googlegroups.com
Well, that code-tag didn't work as expected, so here pastebin :D 

Shimoda

unread,
Apr 22, 2015, 6:22:06 PM4/22/15
to python-fo...@googlegroups.com
Hi,

Please ask QPython problem to their community.
(I didn't understand QPython system.)

BTW, I read LocationFacade code in SL4A.
Location Facade listens all location "gps", "network", and "passive".
But a return timing not defined in their code. It will be returned in FIFO.
Thus, you will not get "gps" in once listen.
I suggest that you make the loop and readLocation sometimes to get "gps" response.

I found these sample code in the facade comment, Is it be usable?

import android, time
droid = android.Android()
droid.startLocating()
time.sleep(15)
loc = droid.readLocation().result
if loc = {}:
  loc = getLastKnownLocation().result
if loc != {}:
  try:
    n = loc['gps']
  except KeyError:
    n = loc['network']
  la = n['latitude']
  lo = n['longitude']
  address = droid.geocode(la, lo).result
droid.stopLocating()


2015年4月22日水曜日 22時18分50秒 UTC+9 Tobias Müller:
Reply all
Reply to author
Forward
0 new messages