← 返回
开发者工具 中文

Locate your position on modern Windows

Geolocate the current Windows machine using the native WinRT Location API (PowerShell 5.1). No external API keys, no browser, no dependencies — uses Windows....
使用原生 WinRT Location API(PowerShell 5.1)对当前 Windows 主机进行地理定位。无需外部 API 密钥、浏览器或依赖项 — 使用 Windows...
cute-omega
开发者工具 clawhub v1.0.1 1 版本 100000 Key: 无需
★ 0
Stars
📥 495
下载
💾 15
安装
1
版本
#latest

概述

Locate your position on modern Windows

Locate the Windows machine using the built-in WinRT Windows.Devices.Geolocation API.

Usage

Save the script below as geolocate.ps1 and run:

powershell.exe -ExecutionPolicy Bypass -File geolocate.ps1

Returns JSON:

{
  "error": false,
  "message": "ok",
  "latitude": 35.6762,
  "longitude": 139.6503,
  "accuracy_m": 1500,
  "timestamp": "2026-01-01T08:00:00.0000000+09:00"
}

Script

# geolocate.ps1 — Windows Geolocation via WinRT (PowerShell 5.1 only)
Add-Type -AssemblyName "System.Runtime.WindowsRuntime"
[Windows.Devices.Geolocation.Geolocator, Windows.Devices.Geolocation, ContentType=WindowsRuntime] | Out-Null

try {
  $geo = New-Object Windows.Devices.Geolocation.Geolocator
  $geo.DesiredAccuracy = [Windows.Devices.Geolocation.PositionAccuracy]::Default
  $geo.DesiredAccuracyInMeters = 100

  $asyncOp = $geo.GetGeopositionAsync()

  $asTask = [System.WindowsRuntimeSystemExtensions].GetMethods() |
    Where-Object {
      $_.Name -eq 'AsTask' -and
      $_.GetParameters().Count -eq 1 -and
      $_.GetParameters()[0].ParameterType.IsGenericType -and
      $_.GetParameters()[0].ParameterType.Name.StartsWith('IAsyncOperation')
    } | Select-Object -First 1

  if (-not $asTask) {
    $output = [Ordered]@{
      error      = $true
      message    = "Cannot resolve AsTask<T> overload"
      latitude   = 0.0
      longitude  = 0.0
      accuracy_m = 0
      timestamp  = [datetime]::Now.ToString("o")
    }
    Write-Output ($output | ConvertTo-Json)
    exit 1
  }

  $genericMethod = $asTask.MakeGenericMethod([Windows.Devices.Geolocation.Geoposition])
  $task = $genericMethod.Invoke($null, @($asyncOp))
  $task.Wait()
  $pos = $task.Result

  $output = [Ordered]@{
    error      = $false
    message    = "ok"
    latitude   = [math]::Round($pos.Coordinate.Point.Position.Latitude, 6)
    longitude  = [math]::Round($pos.Coordinate.Point.Position.Longitude, 6)
    accuracy_m = $pos.Coordinate.Accuracy
    timestamp  = $pos.Coordinate.Timestamp.ToString("o")
  }
  Write-Output ($output | ConvertTo-Json)
} catch {
  $output = [Ordered]@{
    error      = $true
    message    = $_.Exception.Message
    latitude   = 0.0
    longitude  = 0.0
    accuracy_m = 0
    timestamp  = [datetime]::Now.ToString("o")
  }
  Write-Output ($output | ConvertTo-Json)
  exit 1
}

How It Works

  1. Loads System.Runtime.WindowsRuntime for WinRT type resolution.
  2. Instantiates Windows.Devices.Geolocation.Geolocator.
  3. Calls GetGeopositionAsync() — returns IAsyncOperation.
  4. Converts to Task via reflection on System.WindowsRuntimeSystemExtensions.AsTask.
  5. Blocks with .Wait() and outputs JSON.

The location source depends on hardware: GPS (if present), WiFi triangulation, or IP fallback. Typical WiFi accuracy is 1–3 km.

Requirements

  • Windows 10+
  • PowerShell 5.1 (powershell.exe, not pwsh 7)
  • Location must be enabled: Settings → Privacy → Location → On
  • No admin rights needed

Reverse Geocoding

Use the coordinates with any free reverse-geocoding service:

curl -s "https://api.bigdatacloud.net/data/reverse-geocode-client?latitude=35.6762&longitude=139.6503"

Troubleshooting

  • "Location access denied": Enable in Windows Settings → Privacy → Location
  • Timeout / no response: The Location service may be disabled or blocked by group policy
  • Accuracy > 10km: No WiFi/GPS available, falling back to IP-based estimate

版本历史

共 1 个版本

  • v1.0.1 当前
    2026-03-30 05:18 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

developer-tools

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 666 📥 323,786
developer-tools

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 65 📥 179,837
developer-tools

Agent Browser

matrixy
专为AI智能体优化的无头浏览器自动化CLI,支持无障碍树快照和基于引用的元素选择。
★ 425 📥 118,019