Compare commits

..

No commits in common. "master" and "1.0.3" have entirely different histories.

7 changed files with 40 additions and 100 deletions

View File

@ -2,6 +2,7 @@
namespace Zoomyboy\Osm;
use Illuminate\Support\Facades\Http;
class Address
{
@ -12,17 +13,29 @@ class Address
) {
}
public function getCoordinate($serviceClass): ?Point
public function getCoordinate(): ?Coordinate
{
if (!$this->address || !$this->zip || !$this->location) {
return null;
}
return app($serviceClass)->getAddress($this->queryString());
$response = Http::get('https://nominatim.openstreetmap.org/search/'.rawurlencode($this->queryString()).'?'.http_build_query([
'format' => 'json',
'addressdetails' => 1,
]));
if (!$response->ok()) {
return null;
}
$lat = (float) data_get($response, '0.lat');
$lon = (float) data_get($response, '0.lon');
return new Coordinate($lat, $lon);
}
public function queryString(): string
{
return $this->address . ', ' . $this->zip . ' ' . $this->location;
return $this->address.', '.$this->zip.' '.$this->location;
}
}

10
src/Coordinate.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace Zoomyboy\Osm;
class Coordinate
{
public function __construct(public float $lat, public float $lon)
{
}
}

View File

@ -7,7 +7,6 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
class FillCoordsJob implements ShouldQueue
{
@ -23,7 +22,6 @@ class FillCoordsJob implements ShouldQueue
*/
public function __construct(public Geolocatable $model)
{
$this->onQueue('forever');
}
/**
@ -33,21 +31,20 @@ class FillCoordsJob implements ShouldQueue
*/
public function handle()
{
Redis::throttle('osm')->block(0)->allow(1)->every(3)->then(function () {
$address = $this->model->getAddressForGeolocation();
if (!$address) {
$this->model->destroyCoordinate();
$address = $this->model->getAddressForGeolocation();
return;
}
$coordinate = $address->getCoordinate($this->model::geolocationService());
if (!$address) {
$this->model->destroyCoordinate();
if (!$coordinate) {
return;
}
$this->model->fillCoordinate($coordinate);
}, function () {
return $this->release(5);
});
return;
}
$coordinate = $address->getCoordinate();
if (!$coordinate) {
return;
}
$this->model->fillCoordinate($coordinate);
}
}

View File

@ -4,13 +4,11 @@ namespace Zoomyboy\Osm;
interface Geolocatable
{
public function fillCoordinate(Point $point): void;
public function fillCoordinate(Coordinate $coordinate): void;
public function getAddressForGeolocation(): ?Address;
public function destroyCoordinate(): void;
public function needsGeolocationUpdate(): bool;
public static function geolocationService(): string;
}

View File

@ -1,25 +0,0 @@
<?php
namespace Zoomyboy\Osm;
use Illuminate\Support\Facades\Http;
class GoogleService
{
public function getAddress(string $query): ?Point
{
$response = Http::get('https://maps.googleapis.com/maps/api/geocode/json?' . http_build_query([
'address' => $query,
'key' => config('services.osm.google_key'),
]));
if (!$response->ok()) {
return null;
}
return Point::from([
'lat' => (float) data_get($response, 'results.0.geometry.location.lat'),
'lon' => (float) data_get($response, 'results.0.geometry.location.lng')
]);
}
}

View File

@ -1,23 +0,0 @@
<?php
namespace Zoomyboy\Osm;
use Illuminate\Support\Facades\Http;
class OsmService
{
public function getAddress(string $query): ?Point
{
$response = Http::get('https://nominatim.openstreetmap.org/search?' . http_build_query([
'q' => $query,
'format' => 'json',
'addressdetails' => 1,
]));
if (!$response->ok()) {
return null;
}
return count($response->json()) ? Point::from(['lat' => (float) data_get($response, '0.lat'), 'lon' => (float) data_get($response, '0.lon')]) : null;
}
}

View File

@ -1,30 +0,0 @@
<?php
namespace Zoomyboy\Osm;
use Spatie\LaravelData\Data;
class Point extends Data
{
public function __construct(public float $lat, public float $lon)
{
}
public function imageUrl(): string
{
return '/osm-static-maps/?' . http_build_query([
'center' => $this->lon . ',' . $this->lat,
'zoom' => 20,
'maxZoom' => 13,
]);
}
public function markerUrl(string $zoom = '20'): string
{
return '/osm-static-maps/?' . http_build_query([
'geojson' => json_encode(['type' => 'Point', 'coordinates' => [$this->lon, $this->lat]]),
'zoom' => $zoom,
'maxZoom' => $zoom,
]);
}
}