Geocoding: Converting Addresses to Lat/Long (Part 2)

Let’s start with translating addresses into latitude and longitude coordinates. We assume you read the previous parts to fully understand the code sample.

Here is some sample code that takes any number of addresses, and returns their latitude and longitude for you:

'One Microsoft Way, Redmond',
'Bahnhofstrasse 12, Hannover, Germany' |
  ForEach-Object -Begin {$url = 'https://geocode.xyz'
    $null = Invoke-RestMethod $url -S session
  } -Process {
    $address = $_
    $encoded = [Net.WebUtility]::UrlEncode($address)
    Invoke-RestMethod "$url/${encoded}?json=1" -W $session|
      ForEach-Object {
        [PSCustomObject]@{
          Address = $address
          Long = $_.longt
          Lat = $_.latt
        }
      }
  }

The result looks similar to this:

 
Address                              Long       Lat     
-------                              ----       ---     
One Microsoft Way, Redmond           -122.13061 47.64373
Bahnhofstrasse 12, Hannover, Germany 9.75195    52.37799 
 

Twitter This Tip! ReTweet this Tip!

GD Star Rating
loading...