在Python中获取精确的Android GPS位置 [英] Get precise Android GPS-Location in Python

查看:510
本文介绍了在Python中获取精确的Android GPS位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Python获取Android手机的GPS位置(使用QPython3应用程序)。这种工作,但似乎Android中有几个LocationProviders:




  • gps:纯gps位置,慢,耗能,但非常准确,
    ,正是我所需要的。
  • 网络:混合使用gps和wifi / cell定位,但较不准确

  • 被动:类似以上,但完全不使用gps




当我运行我的脚本时(下图),我只能通过network
提供我的位置,但这不够准确。



但我无法找到强制指定LocationProvider的方法。



代码:

 #import needed modules 
import android
import time
import sys,select,os #for loop退出

#初始化android模块
droid = android.Android()

#notify me
droid.makeToast(抓取GPS数据)

print(start gps-sensor ... )
droid.startLocating()

while:
#exit循环钩子
如果select.select中的sys.stdin([sys.stdin],[ ],[],0)[0]:
line = input()
print(exit endless loop ...)
break

#wait对于位置事件
event = droid.eventWaitFor('location',10000).result
if event ['name'] ==location:
try:
#尝试获取GPS位置数据
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
,除KeyError:
#if没有GPS数据,取而代之的是网络位置(不准确)
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 +; +时间戳+; +经度+; +纬度+; +高度+; +速度+; +准确性

print(data)#logging
time.sleep(5)#等待5秒

print(stop gps-sensor ... )
droid.stopLocating()

示例输出(伪坐标):

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

总结:
如何在使用Python的Android中获得精确的GPS位置?



大家好!



编辑:已经测试:




  • 里面/外面

  • 启用/禁用

  • 启用WiFi GPS(运行脚本之前)
h2_lin>解决方案

今天我正在尝试做这样的事情,而且我也遇到了类似的问题。我一遍又一遍地得到相同的输出。长话短说,我发现了这一点。把它放在循环的底部应该可以解决问题。

droid.eventClearBuffer()



输出,你会注意到时间标记都是一样的。清除缓冲区将重置'location'事件返回的对象。


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)

解决方案

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.

这篇关于在Python中获取精确的Android GPS位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆