我一直在做一些研究,但找不到任何关于这个话题的信息。我正在运行一个Windows10版本,它有一个应用程序可以使用的GPS功能,这可以由用户启用或禁用。我想知道如何通过python脚本访问和使用它,假设这是可能的。
注意:我不希望任何解决方案通过IP地理位置服务获取位置。就像在android应用程序中使用移动设备的gps服务一样。
最好是python3库和模块。
发布于 2017-06-09 23:56:59
一方面,在微软的Location API文档中,你可以找到LocationDisp.DispLatLongReport对象,它具有以下属性:
另一方面,通过使用Python模块(或ctype模块),您可以访问Windows API (或任何Windows DLL),因此最终您可以随心所欲地获取经度和经度。
如果你需要帮助,你可以看看来使用模块。
发布于 2020-05-13 11:10:11
这可能是一个迟来的回应,但我最近一直想(主要是出于好奇)实现同样的目标b/c我的公司刚刚购买了支持全球定位系统的Microsoft Surface Go平板电脑(运行win10),以便将其带到野外。我想创建一个小应用程序,记录你在哪里,并根据我们自己的数据库中的信息为你提供周围环境的相关数据。
答案来得有点深入,我真的很想使用pywin32访问Location API,但这一努力很快就泡汤了,因为没有关于这个主题的信息,而且使用.dlls也不是我的兴趣所在。(如果有人有这样的工作示例,请分享!)我猜,由于几乎没有Windows10设备配备GPS,所以几乎没有理由完成这项任务,特别是使用python……
但是关于使用PowerShell命令访问Location API的问题很快就在这个thread中得到了答案。我没有使用另一种语言的PowerShell/shelling命令的经验,但我知道这可能是正确的途径。There is a lot of information out there关于使用Python子进程模块,我应该提一下还有security concerns。
无论如何,这里有一个快速的代码片段可以获取你的位置(我已经验证了一些非常类似的东西可以在我们支持全球定位系统的Microsoft Surface Go上获得3米的精确度)-唯一的事情(总是有一些)是中央处理器往往比全球定位系统更快,并且会默认使用你的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)
https://stackoverflow.com/questions/44400560
复制相似问题