schneespur/app/Services/GeocodingService.php
Michael ee3dbba6cc Initial release v1.0.0
Schneespur — Open-source winter service documentation software (PWA + Admin).
GPS tracking via OwnTracks, weather data, photo evidence, and legally
compliant service records for winter maintenance operators.

License: AGPL-3.0-or-later
2026-05-17 13:33:51 +00:00

43 lines
1.2 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
class GeocodingService
{
public function resolve(string $street, string $zip, string $city): ?array
{
try {
$response = Http::withHeaders([
'User-Agent' => brand_slug().'/1.0 ('.config('app.url').')',
])
->timeout(5)
->get('https://nominatim.openstreetmap.org/search', [
'street' => $street,
'city' => $city,
'postalcode' => $zip,
'country' => 'de',
'format' => 'json',
'limit' => 1,
]);
if ($response->successful()) {
$data = $response->json();
if (! empty($data) && isset($data[0]['lat'], $data[0]['lon'])) {
return [
'lat' => (float) $data[0]['lat'],
'lon' => (float) $data[0]['lon'],
];
}
}
return null;
} catch (ConnectionException|RequestException $e) {
return null;
}
}
}