I had a few minutes to get back to that PowerShell code that found upcoming concerts in St. Louis, and I added a bit. Skip ahead for the code if you most - the most notable feature upgrade includes finding the closest place that the target band is playing and displaying that show’s City/Region and drive time.
Geolocation / Google Distance API
Finding bands who were playing shows in a given city was pretty easy, but what if a band I want to see is playing in a suburb of my target city? Or what about if the band is playing one town over and I would totally be willing to drive the 30 minutes to see the show?
I already have a full list of all the events/venues where each band is playing, I just need to parse them to determine the closest place they’re playing if they’re not playing in my target City. I originally thought about comparing the target city’s latitude and longitude to each event venue’s latitude and longitude. Unfortunately, there’s some serious math that goes into figuring out the distance between two points (See this Stackoverflow discussion to get an idea how crazy things can get). I started down the path of doing a simplified version of comparison to try and limit the calls to an API (Google Maps, Openmaps, Mapquest, etc), but when that failed, I just oped to use Google’s Distance Matrix API as it allows 2.5k requests per day.
Integrating with Google’s API was rather simple. I requested a new API key, then followed their documentation to craft an Invoke-RESTMethod call.
The resulting JSON includes various helpful bits of information like distance in KM: $result.rows[0].elements[0].distance.value and driving duration: $result.rows[0].elements[0].duration.text. Once I had each venue’s distance in KM, a simple comparison enabled me to find the shortest distance, and record the appropriate information (Venue, City/Region, Driving Duration, etc).
Final Tweaks
I added proper help syntax then saved the cmdlet as get-UpcomingConcerts.ps1. Other additions included a progress bar that details which band is currently being examined and a sorted output: first bands that are performing in the target city, then bands that are outside of the city. Various comments, verbose/debug statements, and other formatting fixes went into the finished product found below.
If you wish to use this code yourself, sign up for an API key from Google and enable the Google Distance Matrix API, replacing the value of $key with your API key. You may adjust your target band list in code, or provide it as an array to the -targetBands parameter.