-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-GmapReverseGeoCode.ps1
92 lines (88 loc) · 3.83 KB
/
Get-GmapReverseGeoCode.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Function Get-GmapReverseGeoCode
{
<#
.SYNOPSIS
This function gets address information from either $Latitude and $Longitude or froma $place_id.
.DESCRIPTION
This function gets address information from either $Latitude and $Longitude or froma $place_id.
Uses the Google Places API.
.EXAMPLE
Get-GmapReverseGeoCode -place_id 'ChIJbb_okw9gwokRIboIwTqNnj4'
This will get address information from the place_id ChIJbb_okw9gwokRIboIwTqNnj4 which is the Location
found in the example for Get-GmapPlaceNearby using the keyword cruise.
.EXAMPLE
Get-GmapReverseGeoCode -Latitude '-33.8670522' -Longitude '151.1957362'
This will get address information about a point in Syndey, Australia.
.NOTES
Requires an active Google Place API Key. This key should be set to the moduel variable `$GPlacesApiKey.
Refer to https://developers.google.com/places/web-service/intro
#>
[CmdletBinding(DefaultParameterSetName='PlaceID')]
param
(
[Parameter(Mandatory=$True,
ValueFromPipelinebyPropertyName=$true, ParameterSetName="Location")]
$Latitude,
[Parameter(Mandatory=$True,
ValueFromPipelinebyPropertyName=$true, ParameterSetName="Location")]
$Longitude,
[Parameter(Mandatory=$True,
ValueFromPipelinebyPropertyName=$true, ParameterSetName="PlaceID")]
$place_id,
[Parameter(Mandatory=$False)]
$options
)
Begin{}
Process
{
If($PSCmdlet.ParameterSetName -eq "Location")
{
$Location = "$($Latitude),$($Longitude)"
If($options)
{
$url = $BaseUri + 'geocode/json?latlng=' + $Location + '&' + $(New-GmapQuery -ApiKey $GGeoCodeApiKey -options $options)
}
else
{
$url = $BaseUri + 'geocode/json?latlng=' + $Location + $(New-GmapQuery -ApiKey $GGeoCodeApiKey)
}
Write-Verbose "Sending Url of $url"
$Results = Invoke-RestMethod -Uri $url # Refer to https://developers.google.com/maps/documentation/geocoding/intro for info about properties returned
If($Results.status -eq 'OK')
{
$Results = $Results.results # get the results from json data returned from Google API
$Results | Format-GmapGeoResult # send it to function which adds ScriptProperties and ScriptMethods, sets Default Display Set
}
else
{
Write-Warning "Did not get succcessful return from Google Geocode API endpoint"
$Results
}
}
elseif ($PSCmdlet.ParameterSetName -eq "PlaceID")
{
If($options)
{
$url = $BaseUri + 'geocode/json?place_id=' + $place_id + '&' + $(New-GmapQuery -ApiKey $GGeoCodeApiKey -options $options)
}
else
{
$url = $BaseUri + 'geocode/json?place_id=' + $place_id + $(New-GmapQuery -ApiKey $GGeoCodeApiKey)
}
Write-Verbose "Sending Url of $url"
$Results = Invoke-RestMethod -Uri $url # Refer to https://developers.google.com/maps/documentation/geocoding/intro for info about properties returned
If($Results.status -eq 'OK')
{
$Results = $Results.results # get the results from json data returned from Google API
# $Results
$Results | Format-GmapGeoResult # send it to function which adds ScriptProperties and ScriptMethods, sets Default Display Set
}
else
{
Write-Warning "Did not get succcessful return from Google Geocode API endpoint"
$Results
}
}
}
End{}
}