So my organisation had a need to get accurate latitude and longitude for all of its facilities and had determined that we didn't have a set of accurate records for this.
I determined that we could query Google for this using a web request in the format:
https://maps.googleapis.com/maps/api/place/details/json?placeid=PutYourPlaceIDHere&key=PutYourAPIKeyHere
So, having a list of all of the Google Place IDs for the organisations Google My Business setup, I wrote a little script to invoke a JSON call to Google and pull the latitude and longitude for each facility.
<#
# AUTHOR : Sean Bradley
# CREATED : 08-01-2021
# UPDATED :
# COMMENT : Uses Google Maps API Key to grab GMB Data from Web.
# Updates:
# 1.
#>
#Establish Logging
$RootPath = "C:\Scripts"
$Logfile = "$RootPath\GetGMBData.Log"
Start-Transcript -path $Logfile
#Establish variables
Write-Host "Setting some variables" -ForegroundColor Green
$InputFile = "$RootPath\GMBPlaceIDs.csv"
$OutputFile = "$RootPath\GMBData.csv"
$MapsKey = "PutYourAPIKeyHere"
$MapsURL = "https://maps.googleapis.com/maps/api/place/details/json?placeid="
Write-Host "Doing some preparatory file checks" -ForegroundColor Gray
$FileExists = Test-Path -Path $OutputFile -PathType leaf
If ($FileExists) {
Write-Host "Deleting last export" -ForegroundColor Gray
Remove-item $OutputFile -force | Out-Null
}
# Get Input Data from CSV File
$FileExists = Test-Path -Path $InputFile -PathType Leaf
if ($FileExists) {
Write-Host "Loading $InputFile for processing."
$tblData = import-csv $InputFile
}
else {
Write-Host "$InputFile not found. Stopping script."
exit
}
# Query Google for the required JSON Data
foreach ($row in $tblData)
{
Write-Host "Getting Google Data for " $row.'Centre' " with Google Place ID " $row.'PlaceId'
$QueryURL = $MapsURL + $row.'PlaceId' + '&key=' + $MapsKey
$Webdata = Invoke-RestMethod $QueryURL -Method Get |
Select-Object @{Label = "Centre";Expression = {$row.'Centre'}},
@{Label = "PlaceID";Expression = {$row.'PlaceId'}},
@{Label = "Lat";Expression = {$_.result.geometry.location.lat}},
@{Label = "Lng";Expression = {$_.result.geometry.location.lng}}|
#Export to CSV
Export-Csv -Path $OutputFile -NoTypeInformation -Append
}
Stop-Transcript | out-null
# AUTHOR : Sean Bradley
# CREATED : 08-01-2021
# UPDATED :
# COMMENT : Uses Google Maps API Key to grab GMB Data from Web.
# Updates:
# 1.
#>
#Establish Logging
$RootPath = "C:\Scripts"
$Logfile = "$RootPath\GetGMBData.Log"
Start-Transcript -path $Logfile
#Establish variables
Write-Host "Setting some variables" -ForegroundColor Green
$InputFile = "$RootPath\GMBPlaceIDs.csv"
$OutputFile = "$RootPath\GMBData.csv"
$MapsKey = "PutYourAPIKeyHere"
$MapsURL = "https://maps.googleapis.com/maps/api/place/details/json?placeid="
Write-Host "Doing some preparatory file checks" -ForegroundColor Gray
$FileExists = Test-Path -Path $OutputFile -PathType leaf
If ($FileExists) {
Write-Host "Deleting last export" -ForegroundColor Gray
Remove-item $OutputFile -force | Out-Null
}
# Get Input Data from CSV File
$FileExists = Test-Path -Path $InputFile -PathType Leaf
if ($FileExists) {
Write-Host "Loading $InputFile for processing."
$tblData = import-csv $InputFile
}
else {
Write-Host "$InputFile not found. Stopping script."
exit
}
# Query Google for the required JSON Data
foreach ($row in $tblData)
{
Write-Host "Getting Google Data for " $row.'Centre' " with Google Place ID " $row.'PlaceId'
$QueryURL = $MapsURL + $row.'PlaceId' + '&key=' + $MapsKey
$Webdata = Invoke-RestMethod $QueryURL -Method Get |
Select-Object @{Label = "Centre";Expression = {$row.'Centre'}},
@{Label = "PlaceID";Expression = {$row.'PlaceId'}},
@{Label = "Lat";Expression = {$_.result.geometry.location.lat}},
@{Label = "Lng";Expression = {$_.result.geometry.location.lng}}|
#Export to CSV
Export-Csv -Path $OutputFile -NoTypeInformation -Append
}
Stop-Transcript | out-null