【问题标题】:Location with QPython in Android doesn't return GPS coordinates - only "network"在 Android 中使用 QPython 的位置不会返回 GPS 坐标 - 只有“网络”
【发布时间】:2015-07-28 03:41:22
【问题描述】:

我正在我的安卓手机上使用 QPython 编写一个非常简单的脚本。我想提取我当前位置的 GPS 坐标。但是,该脚本只输出“网络”坐标,而不是 GPS。这是代码sn-p:

#qpy:2
#qpy:console

import androidhelper as android
import time, urllib2
while 1:
    time.sleep(5)
    droid=android.Android()
    droid.startLocating()
    event=droid.eventWaitFor('location', 10000).result
    try:
        lng=event['data']['gps']['longitude']
        lat = event['data']['gps']['latitude']
        print "Longitude: %s ||| Latitude: %s" %(lng,lat) 
    except:
        print "Network Coordinates"

我尝试了最短更新时间,例如:droid.startLocating(10000, 0) 增加了eventWaitFor() 的等待时间,四处移动,但我得到的唯一坐标是“网络”。有没有办法“强制”返回 GPS 坐标?

【问题讨论】:

    标签: android gps qpython


    【解决方案1】:

    在玩了这个之后,事实证明,如果你打电话给droid.getLastKnownLocation() 并且你移动了一下,你会得到更新的 GPS 坐标

    【讨论】:

      【解决方案2】:

      我有一台较旧的 Nexus 7 平板电脑,其中大部分结果都是“网络”,但偶尔会出现“gps”。在一部较新的手机上,我得到了绝大多数“gps”结果,并且每隔几秒就会得到大约 1 次。下面的代码过滤掉了“network”结果,也修复了lat/lon坐标没有被系统更新的问题。

         #-*-coding:utf8;-*-
         #qpy:3
         #qpy:console
      
         import android
         droid = android.Android()
      
         while True:
             droid.startLocating()
             droid.eventWaitFor('location', int(9000))
             location = droid.readLocation().result
             if len(location) > 0:
                 # print('\n', location)
                 if 'gps' in location:
                     print('\n' + location['gps']['provider'])
                     print('Time:' + str(location['gps']['time']))
                     print('Lat:' + str(location['gps']['latitude']))
                     print('Lon:' + str(location['gps']['longitude']))    
             droid.stopLocating()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-02
        • 1970-01-01
        • 1970-01-01
        • 2013-01-20
        • 1970-01-01
        • 2016-09-19
        相关资源
        最近更新 更多