Get precise Android GPS-Location in Python

守給你的承諾、 提交于 2019-12-05 02:11:24

问题


I try to obtain the GPS-Location of my Android Phone in Python (using QPython3 app). This kind of works, but it seems there are several LocationProviders in Android:

  • 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

Problem: When I run my script (below) I only get my location provided by "network" wich is not accurate enough.

But I can't find a way to force a specific LocationProvider.

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()

Sample Output (fake coordinates):

net;1429704519675;37.235065;-115.811117;0;0;23
net;1429704519675;37.235065;-115.811117;0;0;23
net;1429704519675;37.235065;-115.811117;0;0;23

Summarization: How do I get a precise GPS location in Android using Python?

Thanks in advance everyone!

EDIT: already tested:

  • inside / outside
  • enabled / disabled
  • WiFi GPS enabled (before running script)

回答1:


I had the same problem. You Can do something Like this if you want to enforce GPS as LocationProvider

import android, time
droid = android.Android()
droid.startLocating()
print('reading GPS ...')
event=droid.eventWaitFor('location', 10000)
while 1:
    try :
        provider = event.result['data']['gps']['provider']
        if provider == 'gps':
            lat = str(event['data']['gps']['latitude'])
            lng = str(event['data']['gps']['longitude'])
            latlng = 'lat: ' + lat + ' lng: ' + lng
            print(latlng)
            break
       else: continue
   except KeyError:
       continue



回答2:


I was trying to do something like this today and I had a similar problem. I was getting the same output over and over again. Long story short, I discovered this. Putting this in at the bottom of the loop should solve the problem.

droid.eventClearBuffer()

If you look at the original post sample output, you will notice that the time stamps are all the same. Clearing the buffer resets the object returned by the 'location' event.




回答3:


droid = android.Android()
droid.startLocating(0, 0)
event = droid.eventWait(1000).result
if event['name'] == "location":
  print(event['data']['gps'])
droid.stopLocating()


来源:https://stackoverflow.com/questions/29797435/get-precise-android-gps-location-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!