Last active
September 16, 2019 04:58
-
-
Save gravejester/533c11c15244c21ef02930ea75880f9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-GeoLocation { | |
<# | |
.SYNOPSIS | |
Get current location. | |
.DESCRIPTION | |
This function tries to get the latitude and longitude coordinates of the current location, | |
and will look up those coordinates on OpenStreetMap to get address information. | |
.EXAMPLE | |
Get-GeoLocation | |
Will try to get address information about your current location. | |
.EXAMPLE | |
Get-GeoLocation -LatLon | |
Will return the Latitude and Longitude of your current location. | |
.NOTES | |
Author: Øyvind Kallstad | |
Version: 1.0 | |
Date: 09.08.2016 | |
.LINK | |
https://communary.net/ | |
#> | |
[CmdletBinding()] | |
param ( | |
# Number of times the function tries to get the coordinates. Default value is 4. | |
[Parameter()] | |
[int] $NumTries = 4, | |
# Number of milliseconds to sleep between each try. Default value is 1000. | |
[Parameter()] | |
[int] $SleepBetweenTries = 1000, | |
# Use this switch to indicate that you want the Latitude and Longitude returned. | |
# Address lookup will not be performed. | |
[Parameter()] | |
[Alias('ll')] | |
[switch] $LatLon | |
) | |
try { | |
Add-Type -AssemblyName 'System.Device' | |
} | |
catch { | |
Write-Warning 'Unable to load the needed assembly (System.Device).' | |
break | |
} | |
$watcher = New-Object -TypeName System.Device.Location.GeoCoordinateWatcher -ArgumentList 'High' | |
[void]$watcher.TryStart($true, [TimeSpan]::FromMilliseconds(1000)) | |
$count = 0 | |
do { | |
$count++ | |
Start-Sleep -Milliseconds $SleepBetweenTries | |
} while (($watcher.Position.Location.IsUnknown) -or ($count -ge $numTries)) | |
if ($watcher.Position.Location.IsUnknown) { | |
Write-Warning 'Couldn''t get coordinates.' | |
} | |
else { | |
$coord = $watcher.Position.Location | |
if (-not ($LatLon)) { | |
$url = "http://nominatim.openstreetmap.org/reverse?format=json&lat=$($coord.Latitude.ToString().Replace(',','.'))&lon=$($coord.Longitude.ToString().Replace(',','.'))&zoom=18&addressdetails=1" | |
try { | |
$result = Invoke-RestMethod -Uri $url | |
Write-Output ($result.address) | |
} | |
catch { | |
Write-Warning $_.Exception.Message | |
} | |
} | |
else { | |
Write-Output ([PSCustomObject][Ordered] @{ | |
Latitude = $coord.Latitude | |
Longitude = $coord.Longitude | |
}) | |
} | |
} | |
} | |
New-Alias -Name whereami -Value Get-GeoLocation -Force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can loop infinitely. Evaluation on line 61 should be an AND statement and the numerical comparison should be less than or equal or you should switch those variables.
} while (($watcher.Position.Location.IsUnknown) -and ($count -le $numTries))
or
} while (($watcher.Position.Location.IsUnknown) -and ($numTries -ge $count))