Open-GoogleMaps.ps1

<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID b3e6a9d7-4c21-48f0-9a63-2d8c71e5f406
 
.AUTHOR MrDRamos
 
.TAGS google maps location directions
 
.PROJECTURI https://github.com/MrDRamos/PsUtils/blob/master/Internet/Open-GoogleMaps.ps1
 
.RELEASENOTES
Initial release.
 
.PRIVATEDATA
#>


<#
.SYNOPSIS
Opens a Google Maps search or directions page.
 
.DESCRIPTION
Builds a Google Maps URL using one of several parameter sets and then:
  - Launches the user's default browser or
  - Returns the URL if -PassThru is specified.
 
Supported parameter sets:
Query
    Performs a free-form Google Maps search.
Address
    Constructs a search using location name and address components.
Directions
    Opens Google Maps directions to a destination with optional origin.
Coordinates
    Opens a map centered on latitude and longitude.
 
.PARAMETER Query
A free-form Google Maps search string.
Pass multiple unquoted words to search for a phrase
without the need for enclosing it in quotes.
 
#region Address Parameter-Set
.PARAMETER Name
Name of a business, landmark, school, or destination.
 
.PARAMETER Street
Street address component.
 
.PARAMETER City
City component.
 
.PARAMETER State
State or province component.
 
.PARAMETER PostalCode
Postal or ZIP code component.
#endregion Address Parameter-Set
 
#region Directions Parameter-Set
.PARAMETER Destination
Destination address or location.
 
.PARAMETER Origin
Optional starting location.
#endregion Directions Parameter-Set
 
#region Coordinates Parameter-Set
.PARAMETER Latitude
Latitude coordinate.
 
.PARAMETER Longitude
Longitude coordinate.
#endregion Coordinates Parameter-Set
 
.PARAMETER PassThru
Returns the generated URL instead of opening a browser.
 
.EXAMPLE
./Open-GoogleMaps -Query "Pizza near Lake Grove NY"
Performs a free-form search in Google Maps and opens the browser.
 
.EXAMPLE
./Open-GoogleMaps Pizza near Lake Grove NY -PassThru
Returns a URL for the unquoted search phrase.
 
.EXAMPLE
./Open-GoogleMaps -Name "Nesconset Elementary School" `
    -Street "54 Browns Road" `
    -City "Nesconset" `
    -State "NY" `
    -PostalCode "11767"
Searches for the specified location using address components.
 
.EXAMPLE
./Open-GoogleMaps -Latitude 40.852 -Longitude -73.151
Opens Google Maps at the specified coordinates.
 
.EXAMPLE
./Open-GoogleMaps -Destination "54 Browns Road Nesconset NY 11767"
Opens driving directions to the destination.
 
.EXAMPLE
./Open-GoogleMaps -Origin "Lake Grove NY" `
    -Destination "54 Browns Road Nesconset NY 11767"
Opens directions using an explicit starting point.
 
.EXAMPLE
$url = ./Open-GoogleMaps -Query "Smith Haven Mall" -PassThru
Returns the generated URL without opening a browser.
 
.NOTES
Purpose: Lightweight Google Maps URL generation utility.
#>

[CmdletBinding(DefaultParameterSetName = 'Query')]
param(
    [Parameter(
        Mandatory,
        Position = 0,
        ValueFromPipeline,
        ValueFromPipelineByPropertyName,
        ValueFromRemainingArguments,
        ParameterSetName = 'Query'
    )]
    [string[]]$Query,

    [Parameter(
        Mandatory,
        ParameterSetName = 'Address'
    )]
    [string]$Name,

    [Parameter(ParameterSetName = 'Address')]
    [string]$Street,

    [Parameter(ParameterSetName = 'Address')]
    [string]$City,

    [Parameter(ParameterSetName = 'Address')]
    [string]$State,

    [Parameter(ParameterSetName = 'Address')]
    [string]$PostalCode,

    [Parameter(
        Mandatory,
        ParameterSetName = 'Coordinates'
    )]
    [double]$Latitude,

    [Parameter(
        Mandatory,
        ParameterSetName = 'Coordinates'
    )]
    [double]$Longitude,

    [Parameter(
        Mandatory,
        ParameterSetName = 'Directions'
    )]
    [string]$Destination,

    [Parameter(ParameterSetName = 'Directions')]
    [string]$Origin,

    [switch]$PassThru
)



function Open-GoogleMaps
{
    [CmdletBinding(DefaultParameterSetName = 'Query')]
    param(
        [Parameter(
            Mandatory,
            Position = 0,
            ValueFromPipeline,
            ValueFromPipelineByPropertyName,
            ParameterSetName = 'Query'
        )]
        [string[]]$Query,

        [Parameter(
            Mandatory,
            ParameterSetName = 'Address'
        )]
        [string]$Name,

        [Parameter(ParameterSetName = 'Address')]
        [string]$Street,

        [Parameter(ParameterSetName = 'Address')]
        [string]$City,

        [Parameter(ParameterSetName = 'Address')]
        [string]$State,

        [Parameter(ParameterSetName = 'Address')]
        [string]$PostalCode,

        [Parameter(
            Mandatory,
            ParameterSetName = 'Coordinates'
        )]
        [double]$Latitude,

        [Parameter(
            Mandatory,
            ParameterSetName = 'Coordinates'
        )]
        [double]$Longitude,

        [Parameter(
            Mandatory,
            ParameterSetName = 'Directions'
        )]
        [string]$Destination,

        [Parameter(ParameterSetName = 'Directions')]
        [string]$Origin,

        [switch]$PassThru
    )

    process
    {
        switch ($PSCmdlet.ParameterSetName)
        {
            'Query'
            {
                $searchText = $Query -join ' '
                $encodedQuery = [Uri]::EscapeDataString($searchText)
                $url = "https://www.google.com/maps/search/?api=1&query=$encodedQuery"
            }

            'Address'
            {
                $searchText = (
                    @(
                        $Name
                        $Street
                        $City
                        $State
                        $PostalCode
                    ) | Where-Object `
                    {
                        $_
                    }
                ) -join ' '

                $encodedQuery = [Uri]::EscapeDataString($searchText)
                $url = "https://www.google.com/maps/search/?api=1&query=$encodedQuery"
            }

            'Coordinates'
            {
                $encodedQuery = [Uri]::EscapeDataString("$Latitude,$Longitude")
                $url = "https://www.google.com/maps/search/?api=1&query=$encodedQuery"
            }

            'Directions'
            {
                $encodedDestination = [Uri]::EscapeDataString($Destination)
                $url = "https://www.google.com/maps/dir/?api=1&destination=$encodedDestination"

                if ($Origin)
                {
                    $encodedOrigin = [Uri]::EscapeDataString($Origin)
                    $url += "&origin=$encodedOrigin"
                }
            }
        }

        if ($PassThru)
        {
            return $url
        }

        Start-Process $url
    }
}

Open-GoogleMaps @PSBoundParameters