【问题标题】:Get precise Android GPS-Location in Python在 Python 中获取精确的 Android GPS 位置
【发布时间】:2015-06-30 02:28:46
【问题描述】:

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

  • gps:纯gps定位,速度慢,耗能,但非常准确, 正是我需要的。
  • 网络:混合使用 gps 和 wifi/手机定位,速度更快,但准确度较低
  • 被动:与上面类似,但完全不使用 GPS

问题: 当我运行我的脚本(如下)时,我只能得到“网络”提供的位置 这还不够准确。

但我找不到强制使用特定 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 位置?

提前谢谢大家!

编辑:已测试:

  • 内/外
  • 启用/禁用
  • WiFi GPS 已启用(在运行脚本之前)

【问题讨论】:

    标签: android python gps location-provider


    【解决方案1】:

    我遇到了同样的问题。如果你想强制 GPS 作为 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
    

    【讨论】:

    • 如何导入android?
    【解决方案2】:

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

    droid.eventClearBuffer()

    如果您查看原始示例输出,您会注意到时间戳都是相同的。清除缓冲区会重置 'location' 事件返回的对象。

    【讨论】:

      【解决方案3】:
      droid = android.Android()
      droid.startLocating(0, 0)
      event = droid.eventWait(1000).result
      if event['name'] == "location":
        print(event['data']['gps'])
      droid.stopLocating()
      

      【讨论】:

        猜你喜欢
        • 2013-03-25
        • 1970-01-01
        • 2015-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-06
        • 2016-07-30
        • 2016-02-18
        相关资源
        最近更新 更多