Add point data class

This commit is contained in:
philipp lang 2024-07-09 11:20:40 +02:00
parent 132d15f368
commit e924be87df
3 changed files with 9 additions and 9 deletions

View File

@ -13,13 +13,14 @@ class Address
) { ) {
} }
public function getCoordinate(): ?Coordinate public function getCoordinate(): ?Point
{ {
if (!$this->address || !$this->zip || !$this->location) { if (!$this->address || !$this->zip || !$this->location) {
return null; return null;
} }
$response = Http::get('https://nominatim.openstreetmap.org/search/'.rawurlencode($this->queryString()).'?'.http_build_query([ $response = Http::get('https://nominatim.openstreetmap.org/search?' . http_build_query([
'q' => $this->queryString(),
'format' => 'json', 'format' => 'json',
'addressdetails' => 1, 'addressdetails' => 1,
])); ]));
@ -28,10 +29,7 @@ class Address
return null; return null;
} }
$lat = (float) data_get($response, '0.lat'); return count($response->json()) ? Point::from(['lat' => (float) data_get($response, '0.lat'), 'lon' => (float) data_get($response, '0.lon')]) : null;
$lon = (float) data_get($response, '0.lon');
return new Coordinate($lat, $lon);
} }
public function queryString(): string public function queryString(): string

View File

@ -4,7 +4,7 @@ namespace Zoomyboy\Osm;
interface Geolocatable interface Geolocatable
{ {
public function fillCoordinate(Coordinate $coordinate): void; public function fillCoordinate(Point $point): void;
public function getAddressForGeolocation(): ?Address; public function getAddressForGeolocation(): ?Address;

View File

@ -2,7 +2,9 @@
namespace Zoomyboy\Osm; namespace Zoomyboy\Osm;
class Coordinate use Spatie\LaravelData\Data;
class Point extends Data
{ {
public function __construct(public float $lat, public float $lon) public function __construct(public float $lat, public float $lon)
{ {