【问题标题】:Using Windows GPS Location Service in a Python Script在 Python 脚本中使用 Windows GPS 定位服务
【发布时间】:2017-11-08 02:14:24
【问题描述】:

我一直在做一些研究,但找不到有关该主题的任何信息。我正在运行一个 Windows 10 版本,该版本具有可供应用使用的 GPS 功能,用户可以启用或禁用此功能。我想知道如何通过 python 脚本访问和使用它,假设它甚至是可能的。

注意:我不需要任何解决方案来通过 IP 地理定位服务获取位置。就像在 android 应用程序中使用移动设备的 gps 服务一样。

最好是 python3 库和模块。

【问题讨论】:

  • 只有一个应用程序可以同时访问 GPS 数据。位置 API 和 GPS 数据不同。位置 API 使用外部连接,但 GPS 在本地工作。我没有任何包含 GPS 传感器的 win10 设备。举个例子。

标签: python windows geolocation gps


【解决方案1】:

这可能是一个迟来的回应,但我最近(主要是出于好奇)想要实现同样的目标,因为我的公司刚刚购买了支持 GPS 的 Microsoft Surface Go 平板电脑(运行 win10),以便进入该领域.我想创建一个小应用程序,它可以记录您的位置,并根据我们自己的数据库中的信息为您提供有关您周围环境的相关数据。

经过一番挖掘,我得到了答案,我真的很想使用 pywin32 来访问 Location API,但这项努力很快就失败了,因为没有关于该主题的信息,并且使用 .dlls 不是我的最爱茶。 (如果有人有这方面的工作示例,请分享!)我猜由于几乎没有任何 Windows 10 设备配备 GPS,因此几乎没有理由完成这项任务,尤其是使用 python...

但很快就在thread 中找到了答案,关于使用 PowerShell 命令访问 Location API。我对使用另一种语言的 PowerShell/shelling 命令没有太多经验,但我知道这可能是正确的道路。 There isa lotof informationout there关于使用python的子进程模块,我应该提到还有security concerns

无论如何,这里有一个快速的 sn-p 代码,可以获取您的位置(我已经验证了与我们的启用 GPS 的 Microsoft Surface Go 非常相似的东西,可以将精度提高到 3 米) - 唯一的事情(如总有一些东西)是CPU往往比GPS更快,并且会默认使用您的IP / MAC地址,甚至是非常不准确的蜂窝三角测量以尽可能快地获得您的位置(嗯facepalm可能来自2016年? )。因此有等待命令,我实现了一个准确度构建器(可以删除它以搜索所需的准确度)以确保它在接受较粗略的值之前搜索精细的准确度,因为我遇到了它太快地抓取蜂窝位置的问题,即使当GPS可以在同一个地方让我达到3米的精度!如果有人对此感兴趣,请测试/修补并让我知道它是否有效。毫无疑问,像这样的解决方法会出现一些问题,所以要小心。

免责声明:我不是计算机科学专业的学生,​​也不像大多数程序员那样了解。我是一名自学成才的工程师,所以只要知道这是一个人写的。如果您确实对我的代码有见解,请把它交给我!我一直在学习。

import subprocess as sp
import re
import time

wt = 5 # Wait time -- I purposefully make it wait before the shell command
accuracy = 3 #Starting desired accuracy is fine and builds at x1.5 per loop

while True:
    time.sleep(wt)
    pshellcomm = ['powershell']
    pshellcomm.append('add-type -assemblyname system.device; '\
                      '$loc = new-object system.device.location.geocoordinatewatcher;'\
                      '$loc.start(); '\
                      'while(($loc.status -ne "Ready") -and ($loc.permission -ne "Denied")) '\
                      '{start-sleep -milliseconds 100}; '\
                      '$acc = %d; '\
                      'while($loc.position.location.horizontalaccuracy -gt $acc) '\
                      '{start-sleep -milliseconds 100; $acc = [math]::Round($acc*1.5)}; '\
                      '$loc.position.location.latitude; '\
                      '$loc.position.location.longitude; '\
                      '$loc.position.location.horizontalaccuracy; '\
                      '$loc.stop()' %(accuracy))

    #Remove >>> $acc = [math]::Round($acc*1.5) <<< to remove accuracy builder
    #Once removed, try setting accuracy = 10, 20, 50, 100, 1000 to see if that affects the results
    #Note: This code will hang if your desired accuracy is too fine for your device
    #Note: This code will hang if you interact with the Command Prompt AT ALL 
    #Try pressing ESC or CTRL-C once if you interacted with the CMD,
    #this might allow the process to continue

    p = sp.Popen(pshellcomm, stdin = sp.PIPE, stdout = sp.PIPE, stderr = sp.STDOUT, text=True)
    (out, err) = p.communicate()
    out = re.split('\n', out)

    lat = float(out[0])
    long = float(out[1])
    radius = int(out[2])

    print(lat, long, radius)

【讨论】:

    【解决方案2】:

    一方面,从 Microsoft Location API 文档中您会发现,LocationDisp.DispLatLongReport 对象具有以下属性:

    • 海拔高度
    • 海拔高度误差
    • 误差半径
    • 纬度
    • 经度
    • 时间戳

    另一方面,通过使用 Python pywin32 模块(或 ctype 模块),您将可以访问 Windows API(或任何 Windows DLL),因此最终您可以根据需要获得 Lat & Long .

    如果您需要帮助,要使用pywin32 模块,您可以查看here

    【讨论】:

    • 一些具体的例子怎么样
    • 我没有任何带有 GPS 传感器的 win10 设备
    猜你喜欢
    • 1970-01-01
    • 2015-10-03
    • 2019-06-12
    • 1970-01-01
    • 2019-10-13
    • 2013-10-22
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    相关资源
    最近更新 更多