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



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

注意:我不希望通过IP地理位置服务获得任何解决方案。很像在Android应用中使用移动设备的GPS服务。

最好是python3 libs和模块。

一方面,从Microsoft位置API文档中,您将找到具有这些属性的位置disp.displatlongreport对象:

  • 高度
  • altitudeerror
  • Errorradius
  • 纬度
  • 经度
  • 时间戳

另一方面,通过使用Python Pywin32模块(或CTYPE模块(,您将可以访问Windows API(或任何Windows DLL(,因此最后您可以获得LAT&长达您想要的。

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

这可能是一个较晚的响应,但我最近想要(大多是出于好奇心(来实现同样的事情b/c我的公司刚刚购买了启用GPS的Microsoft Surface Go Tables(运行运行(Win10(带出场。我想创建一个小型应用程序,该应用程序记录您所处的位置,并根据我们自己数据库中的信息为周围环境提供相关数据。

答案有点挖掘,我真的想使用pywin32访问位置API,但是这项工作很快就消失了,因为没有关于该主题的信息,并且与.dlls合作并不是我的杯子茶。(如果有人有一个工作的例子,请分享!(我猜想,由于几乎没有任何Windows 10设备配备了GPS,因此几乎没有理由完成任务,尤其是使用Python ...

但是,答案很快就出现在此主题中,以使用PowerShell命令访问位置API。我没有另一种语言的Powershell/炮击命令的经验太多,但我知道这可能是正确的道路。有很多有关使用Python子过程模块的信息,我也应该提到安全问题。

无论如何,这是一个快速的代码段,它将抓住您的位置(我已经验证了与我们的启用GPS的Microsoft Surface一起使用的东西,以使其准确到3米( - 唯一的事情(因为存在总是有什么(是,CPU倾向于比GPS快,并且默认为您的IP/MAC地址,甚至是疯狂的不准确的蜂窝三角剖分,以尽可能快地获得位置(也许是2016年的HMM facepalm?(。因此,有一些等待命令,我实现了一个准确的构建器(可以将其删除以搜索所需的准确性(,以确保它在接受较高的值之前搜索精确度,因为我对IT遇到的问题也太快地抓住了蜂窝位置,即使全科医生可以在同一位置使我获得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)

使用 pip install winsdk

安装winsdk

然后使用此代码(源(:

import asyncio
import winsdk.windows.devices.geolocation as wdg

async def getCoords():
    locator = wdg.Geolocator()
    pos = await locator.get_geoposition_async()
    return [pos.coordinate.latitude, pos.coordinate.longitude]

def getLoc():
    try:
        return asyncio.run(getCoords())
    except PermissionError:
        print("ERROR: You need to allow applications to access you location in Windows settings")

print(getLoc())

它使用Windows.devices.geolocation名称空间。

import winsdk.windows.devices.geolocation as wdg

async def getCoords():
    locator = wdg.Geolocator()
    pos = await locator.get_geoposition_async()
    return [pos.coordinate.latitude, po`enter code here`s.coordinate.longitude]

最新更新