Last active
January 16, 2020 16:34
-
-
Save Cirzen/9c013f83a80a26b41a171ab0c68456ce to your computer and use it in GitHub Desktop.
This file contains hidden or 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-DeviceLocation | |
{ | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $false)] | |
[TimeSpan] | |
$TimeOut = [timespan]::FromSeconds(5), | |
# The accuracy of the reported location. Default relies on Network location (but can still be quite accurate). "High" requires GPS or similar to produce results | |
[Parameter(Mandatory = $false)] | |
[string] | |
[ValidateSet("Default", "High")] | |
$Accuracy = "Default", | |
# Opens Google Maps with the resultant location (if found) | |
[switch] | |
$Map | |
) | |
Begin | |
{ | |
Add-Type -AssemblyName System.Device | |
} | |
End | |
{ | |
$Geo = [System.Device.Location.GeoCoordinateWatcher]::new($Accuracy) | |
if ($Geo.TryStart($false, $TimeOut)) | |
{ | |
$sw = [System.Diagnostics.Stopwatch]::StartNew() | |
While ($Geo.Status -ne "Ready" -and $sw.Elapsed -lt $TimeOut) | |
{ | |
Start-sleep -Milliseconds 30 | |
} | |
if ($Map) | |
{ | |
$Location = $Geo.Position.Location | |
if ([double]::IsFinite($Location.Latitude * $Location.Longitude) ) | |
{ | |
$LatLng = "{0},{1}" -f $Location.Latitude, $Location.Longitude | |
Start-Process "https://google.com/maps/search/?api=1&query=$($LatLng)" | |
} | |
else | |
{ | |
Write-Warning "Unable to retrieve valid values for Latitude and/or Longitude" | |
} | |
} | |
else | |
{ | |
$Geo.Position.Location | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment