So I'm taking a spreadsheet of some 5,000 addresses and sending it off the the Google Maps Geolocation API to get lat/long as well as formatted address values as many are shorthand here.
The problem I'm having is their lack of use of named keys
for me to utilize and positions shifting around based on what's available. For instance, here is an address that's completely usual;
{
"results" : [
{
"address_components" : [
{
"long_name" : "27502",
"short_name" : "27502",
"types" : [ "street_number" ]
},
{
"long_name" : "Antonio Parkway",
"short_name" : "Antonio Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Ladera Ranch",
"short_name" : "Ladera Ranch",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Orange County",
"short_name" : "Orange County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "92694",
"short_name" : "92694",
"types" : [ "postal_code" ]
}
]
Now for instance, here is a location that is within a subpremise:
{
"results" : [
{
"address_components" : [
{
"long_name" : "117",
"short_name" : "117",
"types" : [ "subpremise" ]
},
{
"long_name" : "3401",
"short_name" : "3401",
"types" : [ "street_number" ]
},
{
"long_name" : "North Miami Avenue",
"short_name" : "N Miami Ave",
"types" : [ "route" ]
},
{
"long_name" : "Wynwood",
"short_name" : "Wynwood",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Miami",
"short_name" : "Miami",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Miami-Dade County",
"short_name" : "Miami-Dade County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Florida",
"short_name" : "FL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "33127",
"short_name" : "33127",
"types" : [ "postal_code" ]
}
]
The problem with this is in the first case, address_components[0].short_name
would be the street_number
as I want, but in example 2 address_components[0].short_name
would actually be the lot number.
In a scripted manner, this leaves it impossible to accurately build my JSON as in some areas my value of Zip Code
will be the Street Name because there was a secondary address listing.
How would you recommend I counter this to ensure I'm always getting exactly the data I'm requesting as opposed to whatever happens to be in that position that time?
Below is an example of how this can mess things up:
"731": {
"City": "Noblesville",
"Street": "Norell Ln",
"Type": "dsg",
"StreetNum": "13157",
"Zip": "US",
"State": "Hamilton County",
"Name": "Noblesville ",
"Region": "Ohio Valley",
"Long": "-85.93149269999999",
"StateAbr": "Hamilton County",
"Phone": "3177761687",
"Lat": "39.9901088"
}
This isn't always the case, maybe 200-300 of them are wrong, while the rest are correct, but its very inefficient to have to guess and hope.
For the sake of including some of my script:
$builtAddress = "$street $city $state $zip"
Write-Verbose "Built Address: $builtAddress"
$addressArray.Add($builtAddress) > $null
$req = Invoke-WebRequest "https://maps.googleapis.com/maps/api/geocode/json?address=$builtAddress&key=AIzaSyDRo-UGY91_EiB2DeYzBU21-3FcaqIanPo"
$location = $req.Content | ConvertFrom-Json
$lat = $location.results[0].geometry.location.lat
$long = $location.results[0].geometry.location.lng
$z = $location.results[0].address_components
$name = $z.
$phone = $phone
$region = $region
$objectProps = @{
$store = @{
Name = "$name"
Lat = "$lat"
Long = "$long"
StreetNum = "$streetNum"
Street = "$street"
City = "$city"
State = "$state"
StateAbr = "$stateAb"
Zip = "$zip"
Phone = "$phone"
Region = "$region"
Type = "$type"
}
}
Write-Verbose $store
$jsonObj = New-Object psobject -Property $objectProps | ConvertTo-Json -depth 100 | Out-File C:\Users\admin-dksc104694\Desktop\Map_Data\JSON\$outputFile -Append
Write-Output ',' | Out-File C:\Users\admin-dksc104694\Desktop\$outputFile -Append
}