Custom Google Maps Geocode Powershell CmdLet

by Donn Felker 18. September 2008 09:43

This may look like a repeat... well.. because it is... BUT.. you cannot find the old post. Why? Because, well ... it no longer exists. My old host decided to accidentally delete my account. Yeah. Oops. After that nightmare, I've moved to a new host where the grass is greener, the hippies wear deodorant and well ... wait ... never mind... onto the good stuff...Here's the old post, reposted again.

---------------------------------

Ok, I admit ... I have a fascination with maps. Why? Who knows. I just love knowing where I'm at, where I'm going, not being lost, etc etc etc. Last year I wrote a C# class that would connect to Google Maps and return the latitude and longitude of an address by utilizing the Google Maps API. Get that class

Awhile back I also wrote about the complexity of our industry and about how staying up to speed is quite difficult. Jeffrey Snover (PowerShel/Windows Management Architect) commented on that post and recommend learning PowerShell. I completely agree with what he said. However - I've only scratched the surface of PowerShell and its capabilities and just recently I picked up a copy of Bruce Payettes "Windows PowerShell in Action". So far, its a great book. I'll post a review when I'm done. But as I was reading I got into thinking about how I would like to Geocode addresses from the command line in certain instances. A custom PowerShell cmdlet was in fashion. 

 

The Get-GoogleGeocode CmdLet

At that point, the Get-GoogleGeocode cmdlet was born. I took my code from the C# Google Maps Geocoding class I wrote, added a new property "Address" (which is what I needed to store the address the user supplied) as a simple string and then wrapped it in a cmdlet. 

The result? An awesome cmdlet. 

The cmdlet will take your Google Maps API Key and a list of addresses and it will Geocode them for you. Very simple, yet very powerful. 

Lets take a look at what it can do: 

Examples

 

Note: My Google Maps API key has been hidden in the examples below ... SO YOU DONT STEAL IT! ;)

 

Script Explanation

Click the pictures for full resolution images.

 

 

Example 1:  Gets  the longitude and latitude for one single address and outputs it to the screen.

ps1

PS C:\Development\DF.PowerShell.Cmdlets\bin\Release> Get-GoogleGeocode -apiKey <YourAPIKeyGoesHere> -addresses "2203 E. Empire St. Bloomington, IL 61704"

Address Latitude Longitude
------- -------- ---------
2203 E. Empire St. Bloomington, IL 6... 40.488283 -88.945315

 

Example 2:  Gets the longitude and latitude for multiple addresses and outputs them to the screen. 

ps2

PS C:\Development\DF.PowerShell.Cmdlets\bin\Release> Get-GoogleGeocode -apiKey <YourAPIKeyGoesHere> -addresses "2203 E. Empire St. Bloomington, IL 61704", "8300 Norman Center Dr., Suite 950 Bloomington, MN 55437"

Address Latitude Longitude
------- -------- ---------
2203 E. Empire St. Bloomington, IL 6... 40.488283 -88.945315
8300 Norman Center Dr., Suite 950 Bl... 44.853079 -93.352194

 

Example 3:  (I think this is the REALLY cool one) Opens a file, reads each line and gets the longitude and latitude for each address in the file.

ps3

PS C:\Development\DF.PowerShell.Cmdlets\bin\Release> Get-Content c:\temp\MicrosoftOffices.txt | Foreach-Object {Get-Goog
leGeocode -apikey <YourAPIKeyGoesHere> -addresses $_ }

Address Latitude Longitude
------- -------- ---------
2203 E. Empire St. Bloomington, IL 6... 40.488283 -88.945315
77 W. Wacker Dr., Suite 2300 Chicago... 41.886499 -87.630526
3025 Highland Pkwy., Suite 300 Downe... 41.831445 -88.010731
500 E. 96th St., Suite 460 Indianapo... 39.928171 -86.150754
N19 W24133 Riverwood Dr., Suite 150 ... 43.056940 -88.229463
4601 Westtown Parkway, Suite 136 Wes... 41.610376 -93.711712
10801 Mastin Blvd., Suite 620 Overla... 38.932978 -94.702328
8300 Norman Center Dr., Suite 950 Bl... 44.853079 -93.352194
3 City Place Dr., Suite 1100 St. Lou... 38.670381 -90.433552
13815 FNB Parkway Omaha NE 68154 41.266103 -96.130733

 

Script HELP

If you ever forget about how to run this command, I have included the help files as well. 

Access help by typing: Get-Help Get-GoogleGeocode. This will give you very basic help. For detailed or full help type: Get-Help Get-GoogleGeocode -detailed or  Get-Help Get-GoogleGeocode -full

To use this script, execute the command Get-GoogleGeocode and supply a api key and a list of addresses separated by a comma. 

From help:

PS C:\Development\DF.PowerShell.Cmdlets\bin\Release> get-help get-googlegeocode
NAME
    Get-GoogleGeocode
SYNOPSIS
    This cmdlet will return the latitude and longitude of the addresses that are passed into the cmdlet.
SYNTAX
    Get-GoogleGeocode -ApiKey <String> -Addresses <String[]> [<CommonParameters>]
DETAILED DESCRIPTION
    This cmdlet will return the latitude and longitude of the addresses that are passed into the cmdlet. The apikey is
    required for this to work. Also, you may encounter proxy issues if you require a proxy for access to the internet.
    This cmdlet utilizes the System.Net.WebClient class to access the internet. This cmdlet will connect with the Googl
    e Maps API, ask for the geocoding information and then return it.
    You will need a Google Maps API Key for this. Sign up for one here: http://code.google.com/apis/maps/
    If the latitude and longitude are both ZEROS, this means the address could not be geocoded.
RELATED LINKS
REMARKS
    For more information, type: "get-help Get-GoogleGeocode -detailed".
    For technical information, type: "get-help Get-GoogleGeocode -full".

 

How to Install

Start powershell and navigate to the Release directory run the "install.ps1" script from the command line. It should look like this when you run it: 

install

 

How to uninstall?

Run the uninstall.ps1 script. Running that script should result in something that resembles this:  

uninstall

 

Downloads

C# Solution & Binaries: DF.PowerShell.zip (44.51 kb)

Note: Text File with Addresses from Example 3 is included in solution download.


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | PowerShell

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Donn Felker

Senior Consultant
MCTS
ScrumMaster
Agile Practitioner

About Me | Books I Recommend

Gotta Pay The Bills


Tag cloud

    Popular Posts

    RecentComments

    Comment RSS

    Calendar

    <<  December 2008  >>
    MoTuWeThFrSaSu
    24252627282930
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234

    View posts in large calendar