Powershell:在 Windows 10 中获取 GPS 坐标 - 使用 Windows 位置 API?

问题描述 投票:0回答:3

我正在寻找一种使用 Windows 位置 API 从 powershell 脚本收集内部 GPS 数据的方法,因为 Windows 位置平台不再能够满足需求。之前有一个 com 对象可以使用。

有没有办法在 Windows 10 中完成此操作?

powershell geolocation gps windows-10 windows-locationapi
3个回答
21
投票

使用

System.Device.Location
方法的示例,这应该就是您正在寻找的。

Add-Type -AssemblyName System.Device #Required to access System.Device.Location namespace
$GeoWatcher = New-Object System.Device.Location.GeoCoordinateWatcher #Create the required object
$GeoWatcher.Start() #Begin resolving current locaton

while (($GeoWatcher.Status -ne 'Ready') -and ($GeoWatcher.Permission -ne 'Denied')) {
    Start-Sleep -Milliseconds 100 #Wait for discovery.
}  

if ($GeoWatcher.Permission -eq 'Denied'){
    Write-Error 'Access Denied for Location Information'
} else {
    $GeoWatcher.Position.Location | Select Latitude,Longitude #Select the relevent results.
}

0
投票

确保位置已设置。否则它会输出“拒绝访问位置信息”


0
投票

如果使用

System.Device.Location.GeoCoordinateWatcher
的接受答案抛出拒绝访问位置信息,则 Windows 向应用程序提供位置的功能可能已被禁用。要打开此功能,请在 Windows 中转至:

开始 > 设置 > 隐私 > 位置

打开允许访问此设备上的位置

打开允许应用程序访问您的位置

© www.soinside.com 2019 - 2024. All rights reserved.