我一直在做一些研究,但无法找到有关该主题的任何信息。我正在运行具有GPS功能的 Windows 10 版本供应用程序使用,可以由用户启用或禁用。我想知道如何通过python脚本访问和使用它,假设它甚至是可能的。
注意:我不希望任何解决方案通过IP地理定位服务获取位置。就像在Android应用程序中使用移动设备的gps服务一样。
最好是python3 libs和modules。
答案 0 :(得分:4)
一方面,您可以在Microsoft Location API文档中找到具有以下属性的LocationDisp.DispLatLongReport对象:
另一方面,通过使用Python pywin32模块(或ctype模块),您将可以访问Windows API(或任何Windows DLL),最后您可以获得Lat& amp ;只要你愿意。
答案 1 :(得分:2)
这可能是一个较晚的响应,但是我最近想(主要出于好奇)要实现同样的目的b / c我的公司刚刚购买了启用GPS的Microsoft Surface Go平板电脑(运行win10)并带到现场。我想创建一个小型应用程序,该应用程序可以记录您的位置,并根据我们自己数据库中的信息为您提供周围环境的相关数据。
答案有点复杂,我确实想使用pywin32来访问Location API,但是这项工作很快就失败了,因为没有关于该主题的信息,使用.dlls并不是我的本事。茶。 (如果有人有一个可行的例子,请分享!)我猜想,由于几乎所有Windows 10设备都没有配备GPS,因此几乎没有理由来完成这项任务,尤其是使用python ...
但是有关使用PowerShell命令访问Location API的答案thread很快就得到了答案。我没有使用其他语言的PowerShell / shell命令的丰富经验,但是我知道这可能是正确的方法。 There is a lot of information out there关于使用python的子进程模块,我也应该提到security concerns。
无论如何,这是一段简短的代码片段,它将抓住您的位置(我已经验证了一些非常类似的功能,这些功能与启用GPS的Microsoft Surface Go可以达到3米的精度)-唯一的优势(因为总是有些事情)是CPU趋向于比GPS更快,并且将默认使用您的IP / MAC地址或什至是非常不准确的蜂窝三角剖分来尽可能快地获得位置(也许是从2016年开始的facepalm? )。因此,有一些等待命令,我实现了一个精度生成器(可以删除精度搜索器以搜索所需的精度),以确保它在接受较粗的值之前先搜索精细的精度,因为我在抓取蜂窝位置时遇到了问题,即使在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)