More With Google Maps
Had a great time creating the Out-LastEarthquake.ps1 script, but let’s face it, other than it’s cool factor it doesn’t have much use. But what we do have is the framework for the ability to place markers on a Google Map, and there are definitely some uses for that. I created a proof of concept script that shows just what you could do.
Longitude and Latitude
The great thing about the Last Earthquake script is the data feed we had already had the longitude and latitude data in it, but to have any useful application the more likely data we’re going to have is address. So we need to figure out a way of translating address information to longitude and latitude. I really wanted to use Google for this, because one thing I’ve learned over the years is scripts often outlast the time you expect them to. That means if you are using an outside source you want it to be around for a long time. There are a lot of great websites out there, but so many of them just don’t last and I wanted to make sure this one does. Besides, we’re using the Google Maps API so it just makes sense to keep it in the house.
A quick Google search and I locate inside the Google Maps API a simple feed that will do this translation for you without too much trouble! It’s called Geocoding (translating an Address to coordinates) and is pretty easy to use. First, let’s test with this address:
4 Yawkey Way
Now, no peeking! To find out what this is you have to use your own script (or the one below)! First thing is we need to remove the comma’s and replace the space’s with the “+” symbol. For this proof of concept I just omitted the comma from the data, and I get to use a new method of inputting data into a script that I ran across a couple of weeks ago. But first, let’s do some Geocoding and see what we get!
In this example I pulled the data into $Geocode and then went exploring. Everything’s under the Results property, including a very useful property called formatted_address but chasing down the geometry property I eventually found the longitude and latitude data we’re looking for! And that’s really all there is, isn’t it? The rest is just window dressing, such as wrapping this in a loop, putting the actual HTML code (taken from the Last Earthquake script) and we’re all set. We now have the ability to create web pages that will have a map on it and points of our choosing all plotted out.
So let’s get some test data and start playing. There will come a time that you want to use some test data, but you may not want to create a separate CSV file and input it every time. It’s just a pain and your test folder ends up cluttered with tons of miscellaneous files. Admittedly, there are worse problems in the world but here’s a technique for getting data into your script without that extra file.
It starts with a here-string that you format like a CSV (including the header row) and then use the ConvertFrom-CSV to convert that here-string into a collection of objects. Here’s the example I used for this script:
As a proof of concept I added some labels and some other data just to show how things can be done. In most scripts you’ll see a line like $MyData = $Data | ConvertFrom-CSV and that’s fine, as $MyData will now be a collection of objects with our data in it but I like to cut out any variables I don’t really need. I only need to run through this data once so why keep a variable with the data in it for the entire lifetime of the script?!
Now we need to loop through that data and create our PSCustomObject with what we need in it:
I also put the second set of data in an ordered list so it will be numbered (you’ll see why soon). So now I just need to create the $TableInfo string to have the data in it we need:
Pretty simple really, each <li> in the ordered list represents a record in our data. I discovered that if I wrap all of my data in a <p></p> and then use <br> to do new lines within it it will give me new lines without 1.5 line spacing that HTML always uses! May not sound like much but I’ve been trying to figure that out for along time.
So we have our map with all of the standard markers on it, and a numbered list to the right with our data on it. Wouldn’t it be nice if we could create markers with numbers on them so you could correlate the data with the placement on the map? Turns out, Google has that built into the API as well, in fact you can insert just about any data into this!
The image variable (line 1) simply points to the place where the image can be found, which is a dynamic image created by Google based on the data you pass to it. So I take advantage of the loop I’m already using to place all the markers to feed the variable into the image creation API. Easy peesy!
The final output is a simple map with coordinates to the left, data to the right. Imagine a display screen in your Customer Service area or warehouse showing the last 10 customers shipped to? Or maybe you have drivers that you want to show people where they’re going today? Maybe you’re a service provider and want to show a map of all the clients being visited today with the times on the right? For these kinds of visualizations this script would work very well. Of course, you’d have to work out where the data is coming from and what format it’s in but you can see where we’re going with this one.
Here’s the full proof of concept for those who are interested: