diff --git a/src/App/AccountPages.php b/src/App/AccountPages.php index 8afb08d..cf3b0a5 100644 --- a/src/App/AccountPages.php +++ b/src/App/AccountPages.php @@ -197,8 +197,12 @@ final class AccountPages $region = trim((string)($_POST['region'] ?? '')); $lat = isset($_POST['lat']) && $_POST['lat'] !== '' ? (float)$_POST['lat'] : null; $lng = isset($_POST['lng']) && $_POST['lng'] !== '' ? (float)$_POST['lng'] : null; - if ($lat === null || $lng === null) { - [$lat, $lng] = self::geocodeAddress($street, $zip, $city, $region); + $needsGeocode = ($lat === null || $lng === null || $region === ''); + if ($needsGeocode) { + [$geoLat, $geoLng, $geoRegion] = self::geocodeAddress($street, $zip, $city, $region); + if ($lat === null) { $lat = $geoLat; } + if ($lng === null) { $lng = $geoLng; } + if ($region === '' && $geoRegion) { $region = $geoRegion; } } if ($action === 'event_add') { @@ -352,7 +356,7 @@ final class AccountPages $region ?: null, ]); if (!$parts) { - return [null, null]; + return [null, null, null]; } $query = implode(', ', $parts); @@ -372,12 +376,14 @@ final class AccountPages $resp = @file_get_contents($url, false, $ctx); if ($resp === false) { - return [null, null]; + return [null, null, null]; } $json = json_decode($resp, true); if (!is_array($json) || empty($json[0]['lat']) || empty($json[0]['lon'])) { - return [null, null]; + return [null, null, null]; } - return [round((float)$json[0]['lat'], 7), round((float)$json[0]['lon'], 7)]; + $addr = $json[0]['address'] ?? []; + $regionGuess = $addr['city_district'] ?? $addr['suburb'] ?? $addr['state'] ?? $addr['county'] ?? $addr['region'] ?? $addr['state_district'] ?? null; + return [round((float)$json[0]['lat'], 7), round((float)$json[0]['lon'], 7), $regionGuess]; } }