# encoding: ascii
# api: powershell
# title: Find-GeoCode
# description: A quick demonstration of using the MapPoint (aka Bing Maps?) web services to get addresses from Lat,Long pairs and vice-versa. Requires you to sign up for (at least) the free developer account in order to use the API.
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Find-ReverseGeoCode
# x-poshcode-id: 1422
# x-archived: 2014-12-15T09:47:33
# x-published: 2009-10-26T14:28:00
#
#
$mappoint = New-WebServiceProxy http://staging.mappoint.net/standard-30/mappoint.wsdl -Namespace MapPoint
$FindService = new-object MapPoint.FindServiceSoap
# You need an account, sign up here: https://mappoint-css.live.com/mwssignup
$FindService.Credentials = Get-Credential 

function Find-ReverseGeoCode( [double]$Latitude, [double]$longitude, [switch]$force  ) 
{
   $Coords = new-object MapPoint.LatLong
   $Coords.Latitude = $Latitude
   $Coords.Longitude = $longitude
   $Locations = $FindService.GetLocationInfo( $Coords, "MapPoint.NA", $null)

   if($force) {
      return $Locations
   } elseif($Locations[0].Address) {
      return $Locations[0].Address # .FormattedAddress + " - " + $Locations[0].Address.CountryRegion
   } else {
      return $Locations | ?{ $_.Entity.TypeName -eq "PopulatedPlace" } # | %{ $_.Entity.DisplayName }
   }
}

function Find-GeoCode( $address, $country = "US" ) 
{
   $spec = new-object MapPoint.FindAddressSpecification
   $spec.DataSourceName = "MapPoint.NA"
   $spec.InputAddress = $FindService.ParseAddress( $address,  $country )
   
   $Found = $FindService.FindAddress( $spec )
   if($Found.NumberFound) {
      $found.Results | Select -expand FoundLocation | Select -Expand LatLong
   }
   #  if($Found.NumberFound -gt 1) {
   #     $found.Results| Select -expand FoundLocation | Select -Expand Address
   #  }
}

## This is how non-geography geeks think of them:
Set-Alias Find-Address Find-ReverseGeoCode
Set-Alias Find-Coordinates Find-GeoCode
