Advertisement

python实现卫星定位_在Python中获取精确的Android GPS位置

阅读量:

I am trying to retrieve the GPS location data from my Android smartphone using the QPython3 app. In general, this approach functions well, but there are multiple instances of LocationProviders available in Android systems.

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)

解决方案

Today, I attempted to replicate a process akin to this and encountered a comparable issue. The same output kept recurring consistently. To summarize, I identified an issue. The solution is to place this at the bottom of the loop.

droid.eventClearBuffer()

观察原始帖子样本输出时你会发现所有时间戳都相同。清除缓存会重置由‘location’事件返回的对象。

全部评论 (0)

还没有任何评论哟~