Get precise Android GPS-Location in Python(在 Python 中获取精确的 Android GPS 位置)
问题描述
我尝试在 Python 中获取我的 Android 手机的 GPS 位置(使用 QPython3 应用程序).这种工作,但似乎Android中有几个LocationProviders:
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:纯gps定位,速度慢,耗能,但非常准确,正是我需要的.
- 网络:混合使用 gps 和 wifi/蜂窝定位,速度更快,但准确度较低
- 被动:与上面类似,但完全不使用 GPS
- 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.
但我找不到强制使用特定 LocationProvider 的方法.
But I can't find a way to force a specific LocationProvider.
代码:
# 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()
样本输出(假坐标):
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
总结:如何使用 Python 在 Android 中获取精确的 GPS 位置?
Summarization: How do I get a precise GPS location in Android using Python?
提前谢谢大家!
已测试:
- 里面/外面
- 启用/禁用
- 启用 WiFi GPS(在运行脚本之前)
推荐答案
droid = android.Android()
droid.startLocating(0, 0)
event = droid.eventWait(1000).result
if event['name'] == "location":
print(event['data']['gps'])
droid.stopLocating()
这篇关于在 Python 中获取精确的 Android GPS 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!