Browse Source

add postal code when geocoding something that is missing one

main
Aaron Parecki 6 years ago
parent
commit
10e5e07c2a
No known key found for this signature in database GPG Key ID: 276C2817346D6056
1 changed files with 13 additions and 4 deletions
  1. +13
    -4
      p3k/Geocoder.php

+ 13
- 4
p3k/Geocoder.php View File

@ -61,13 +61,22 @@ class Geocoder {
if($attributes->Country) if($attributes->Country)
$result->countryName = $attributes->Country; $result->countryName = $attributes->Country;
if($attributes->Postal)
if($attributes->Postal) {
$result->postalCode = $attributes->Postal; $result->postalCode = $attributes->Postal;
} else {
// If no postal code is returned, but there was coordinates, then reverse geocode to find the postal code
if($geometry) {
$rev = self::_reverse($result->latitude, $result->longitude);
if($rev && property_exists($rev->address, 'Postal')) {
$result->postalCode = $rev->address->Postal;
}
}
}
if($location->name) if($location->name)
$result->fullAddress = $location->name; $result->fullAddress = $location->name;
return $result;
return $result;
} }
private static function _reverse($lat, $lng) { private static function _reverse($lat, $lng) {
@ -81,7 +90,7 @@ class Geocoder {
if($result == FALSE) if($result == FALSE)
return FALSE; return FALSE;
return json_decode($result);
return json_decode($result);
} }
private static function _geocode($input) { private static function _geocode($input) {
@ -103,7 +112,7 @@ class Geocoder {
if($result == FALSE) if($result == FALSE)
return FALSE; return FALSE;
return json_decode($result);
return json_decode($result);
} }
} }

Loading…
Cancel
Save