Installation:
composer require arsenyru/geodata
Note: Requires Symfony 2.3+ (not Laravel-compatible natively; see Integration Patterns for Laravel adaptation).
First Use Case:
Geodata\Coordinate\Coordinate class to handle lat/long conversions.
use Geodata\Coordinate\Coordinate;
$coordinate = new Coordinate(55.7558, 37.6173); // Moscow
$formatted = $coordinate->toString(); // "55.7558, 37.6173"
Key Classes to Explore:
Geodata\Coordinate\Coordinate: Core class for lat/long operations.Geodata\Distance\Distance: Calculate distances between coordinates.Geodata\GeoJson\GeoJson: Work with GeoJSON-formatted data.Service Provider:
Register the package as a Laravel service provider (e.g., GeodataServiceProvider):
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Geodata\Coordinate\Coordinate;
class GeodataServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('geodata.coordinate', function ($app) {
return new Coordinate(...); // Inject defaults if needed
});
}
}
Facade (Optional): Create a facade for cleaner syntax:
// app/Facades/Geodata.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Geodata extends Facade
{
protected static function getFacadeAccessor() { return 'geodata.coordinate'; }
}
Usage:
$distance = app('geodata.coordinate')->distanceTo(new Coordinate(...));
Request/Response Handling:
$lat = request()->input('lat');
$lng = request()->input('lng');
$coordinate = new Coordinate($lat, $lng);
return response()->json([
'data' => (new GeoJson())->createPoint($coordinate)
]);
Model Bindings: Bind coordinates to Eloquent models:
use Geodata\Coordinate\Coordinate;
class Location extends Model
{
protected $casts = [
'coordinates' => 'array',
];
public function getCoordinateAttribute()
{
return new Coordinate($this->latitude, $this->longitude);
}
}
Symfony Dependency:
Symfony\Component\Form). Laravel users must mock or abstract Symfony dependencies.TCPDF Requirement:
tecnick.com/tcpdf for PDF generation (unused in most Laravel apps). Exclude it via:
composer require arsenyru/geodata --ignore-platform-req=tecnick.com/tcpdf
Coordinate Validation:
if (!$coordinate->isValid()) {
throw new \InvalidArgumentException("Invalid coordinates");
}
GeoJSON Limitations:
GeoJson class lacks Laravel-specific features (e.g., no database integration). Extend it for custom needs:
class LaravelGeoJson extends \Geodata\GeoJson\GeoJson
{
public function toDatabaseArray()
{
return $this->toArray(); // Custom serialization
}
}
Coordinate Precision:
$coordinate = new Coordinate(55.7558, 37.6173, 4); // 4 decimal places
Distance Units:
Distance::MILES for miles:
$distance = $coordinate->distanceTo($otherCoordinate, Distance::MILES);
Performance:
Coordinate objects repeatedly. Cache or reuse instances:
$cacheKey = "coord:{$lat}:{$lng}";
$coordinate = cache()->remember($cacheKey, now()->addHours(1), fn() => new Coordinate($lat, $lng));
Custom Distance Calculators:
Geodata\Distance\Distance for alternative algorithms (e.g., Haversine):
class HaversineDistance extends Distance
{
protected function calculate($lat1, $lon1, $lat2, $lon2)
{
// Custom Haversine formula
}
}
Database Storage:
POINT field in PostgreSQL:
// Migration
$table->geometry('location');
// Model
public function setCoordinatesAttribute($value)
{
$this->attributes['location'] = "POINT({$value->longitude} {$value->latitude})";
}
API Resources:
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use Geodata\GeoJson\GeoJson;
class LocationResource extends JsonResource
{
public function toArray($request)
{
return [
'geometry' => (new GeoJson())->createPoint($this->coordinate),
'properties' => $this->properties,
];
}
}
How can I help you explore Laravel packages today?