Query Google Maps API Using Windows PowerShell

Google Maps API lets you make query information elevation data using WGS84 coordinates. All you have to do is construct a URL with the coordinates, and then Google will return a JSON file. A JSON file is basically a text file, with some extra structure, in the form of some keywords, brackets, braces, and semi-colons.

First you need to use a program that will send a web request in the form of a URL to Google, and then wait for Google’s reply, in the form of a JSON. I used the (new) Windows PowerShell. You probably have Windows PowerShell on your computer, but you might need to get the latest version in order to use the Invoke-WebRequest cmdlet, which we’ll cover later. (I’m not sure what a cmdlet is, but just go with it.) The only reason I suggest using Invoke-WebRequest via Windows PowerShell, and not wget via Linux, is that I’m guessing that you’re at work, and that you have to use Windows.

Once you’ve gotten the latest version of Windows PowerShell (PS), you have to take off the parental controls so that you can use it for stuff.

  • Run PS as an Administrator. That involves looking for it in the Start menu, and then right-clicking on it and selecting ‘Run as Administrator’.
  • Type Set-ExecutionPolicy Unrestricted, and hit enter. This tells PS to run any and all scripts without asking for authorization or verification or permission or whatever. If that sounds scary, you can always change it back later.

The URL that I mentioned you will be using looks like this,

http://maps.googleapis.com/maps/api/elevation/json?locations=32.000000,-102.000000&sensor=false

You will replace the 32.000000 and -102.000000 with your latitude and longitude, this will put you somewhere in West Texas. The &sensor=false tells Google that you’re using a desktop computer somewhere and that it shouldn’t try to pick up data from your device’s sensor, as though you were doing this from a smart phone or something.

To hit up Google for those (elevation) digits, you put that URL in the PowerShell Invoke-WebRequest cmdlet as,

( Invoke-WebRequest "http://maps.googleapis.com/maps/api/elevation/json?locations=32.000000,102.000000&sensor=false" ).Content > data.json

First, Invoke-WebRequest will contact Google asking for elevation data at some set of coordinates using the WGS84 datum. Next, .Content will wait for and catch Google’s response in the form of a JSON file. Finally, > data.json will write a JSON file to the current directory. If you don’t know what the current, or working directory is, type pwd at the PS command line. The command pwd is short for print working directory, and it will return the working directory where your command put the data.json file.

If you need to retrieve multiple elevations, you should use a Start-Sleep cmdlet after each Invoke-WebRequest request. I’d recommend sleeping for a second, which will give you enough time to get some coffee if you have enough queries.

You can then open data.json with either Notepad, or even better, Notepad++, which is a free text editing program with extra functionality.

One thought on “Query Google Maps API Using Windows PowerShell”

Comments are closed.