这可能是一个迟来的回应,但我最近(主要是出于好奇)想要实现同样的目标,因为我的公司刚刚购买了支持 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)