在Python脚本中使用Windows GPS定位服务

M.M*_*M.M 8 python windows gps geolocation

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

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

最好是python3库和模块.

小智 6

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

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

但答案很快就出现在有关使用 PowerShell 命令访问 Location API 的线程中。我对使用其他语言的 PowerShell/shell 命令没有太多经验,但我知道这可能是正确的路径。有 很多 关于使用 python 子进程模块的信息 ,我应该提到也存在安全问题。

无论如何,这里有一个快速的代码片段,可以获取您的位置(我已经验证了与我们支持 GPS 的 Microsoft Surface Go 非常相似的东西,可以将精确度达到 3 米) - 唯一的事情(因为总有一些事情) CPU 往往比 GPS 更快,并且会默认使用您的 IP/MAC 地址,甚至是极其不准确的蜂窝三角测量,以尽可能快地获取您的位置(嗯,也许是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)
Run Code Online (Sandbox Code Playgroud)


A S*_*ANI 5

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

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

另一方面,通过使用 Python pywin32模块(或ctype模块),您将可以访问 Windows API(或任何 Windows DLL),因此最终您可以获得所需的纬度和经度。

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

  • 举几个具体的例子 (2认同)