Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Geographical Calculator Laravel Package

kmlaravel/laravel-geographical-calculator

A Laravel/PHP package for geographical calculations, offering distance and radius queries between coordinates using Haversine and related formulas. Easily find nearby records, compute distances in multiple units, and add location-aware features to your Eloquent models with a simple, fluent API.

View on GitHub
Deep Wiki
Context7

Getting Started

  • Install with Composer and confirm Laravel auto-discovery is picking up the service provider. If the package ships a config file, publish it early with php artisan vendor:publish so coordinate/unit defaults are explicit.
  • Read the README’s value-object examples first: the package revolves around points and areas/polygons. The quickest mental model is “construct a Point, construct or load an Area, then ask spatial questions.”
  • First use case: geofencing. Create a point from a user or address coordinate and call the new “point is located in a given area” check before allowing a delivery, booking, or service request. This is the lowest-risk way to validate installation, coordinate order, and area setup.
  • Second use case: logistics/analytics. Given a reference point (warehouse, driver, store) and a set of candidate points (customers, listings, vehicles), use the new v2.2.0 farthest-point helper to answer “which candidate is furthest from my reference point?” without writing a manual distance loop.
  • v2.2.0 is additive: the new farthest-point and point-in-area capabilities do not appear to introduce breaking changes. Existing distance and point workflows should continue to work; review your test suite if you already had custom implementations of either behavior, since you can now replace them with package-native calls.

Implementation Patterns

  • Point-in-area guard pattern. Validate and normalize coordinates at the boundary of your application (form request or DTO), construct a Point, then perform the area check in a dedicated service or action class. Return a domain result such as Serviceable/OutOfArea rather than a raw boolean so controllers and jobs stay readable.
  • Farthest-point selection. Load candidate points as a collection, construct the reference point once, and ask the package for the farthest candidate. This fits well in a query/action object: fetch candidates from Eloquent, map them to point objects, call the farthest-point method, then map the result back to the original model. Keep the original model key alongside the point so you do not have to reverse-lookup by coordinates.
  • Geofencing checks in requests and jobs. Use the area check in form requests for immediate user feedback, but repeat it in the queued job or command that actually mutates data. That protects against stale area definitions and direct job dispatch.
  • Eloquent integration. If you store coordinates as separate latitude/longitude columns, add an accessor or custom cast that returns a package point object. Avoid constructing points repeatedly inside loops; hydrate once and reuse.
  • Area definitions as configuration or database records. For delivery zones, service areas, or admin regions, keep area boundaries in config or a dedicated table and resolve them through a repository. Cache resolved area objects if the package objects are immutable and the boundaries change infrequently.
  • Testing pattern. Use known fixtures: a square area and points clearly inside, outside, and on the boundary. For farthest-point behavior, test an empty candidate set, a single candidate, ties, and candidates on opposite sides of the reference point. These cases expose unit, coordinate-order, and boundary assumptions early.
  • Composition with existing distance APIs. The farthest-point method is a selection helper, not a replacement for distance calculation. If you need both the winning point and the actual distance, call the distance API on the result rather than reimplementing the comparison.
  • API/resource layer. Serialize only the fields consumers need: point identifier, latitude, longitude, and computed distance if relevant. Do not leak internal area polygon structures unless the frontend genuinely needs to render them.

Gotchas and Tips

  • Coordinate order is the classic failure mode. Confirm whether the package expects latitude/longitude or longitude/latitude, and whether constructors accept floats, strings, or arrays. A point-in-area result that is always false is often a swapped-axis bug, not a broken area.
  • Boundary semantics matter. “Located in a given area” may treat points exactly on an edge differently from points strictly inside. Test edge cases explicitly and document the behavior your application relies on. For legal or billing boundaries, do not assume inclusive behavior.
  • Empty candidate sets for farthest-point queries. Decide whether the package throws, returns null, or returns a sentinel. Wrap the call in your own application-level check so an empty result does not become an unhandled exception in a report or job.
  • Ties are not always deterministic. If two candidates are equidistant from the reference point, the returned “farthest” point may depend on iteration order. If business rules require a tie-breaker (lowest ID, earliest timestamp, alphabetical), implement it in your selection layer.
  • Farthest-point selection is O(n). For large candidate sets, prefetch and filter aggressively before constructing point objects. Spatial indexes in the database are still your friend: narrow by bounding box or region first, then run the exact farthest-point calculation in PHP.
  • Distance units and Earth model assumptions. If you compute distances after selecting the farthest point, check whether the package returns meters, kilometers, or miles and what Earth radius/projection it assumes. Do not mix package distances with database ST_Distance results without normalizing units.
  • Floating-point comparisons. Avoid strict equality on calculated distances or coordinates. Use an epsilon when asserting results in tests, and round only at the presentation layer.
  • Dateline and polar edge cases. Simple planar or haversine implementations can behave surprisingly near the antimeridian or poles. If your user base is global, add fixtures for these regions rather than discovering the issue in production.
  • Configuration quirks. Look for config keys controlling default units, precision, or coordinate order. Publish the config and set them explicitly in each environment instead of relying on package defaults.
  • Debugging workflow. When an area check fails, dump the normalized point coordinates, the area’s bounding box, and the raw boundary vertices. Compare against a known-good external tool or a hand-built fixture. Log the point and area identifiers, not just a boolean.
  • Extension points. If the package exposes interfaces or base classes for points/areas, prefer extending or composing them in your domain layer rather than scattering package objects throughout controllers and models. This keeps future upgrades isolated.
  • Upgrade hygiene. Even though v2.2.0 appears additive, run your existing geospatial tests after upgrading and remove any duplicate custom farthest-point or point-in-area helpers. Replacing bespoke math with the package-native methods reduces maintenance and usually improves correctness.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core
nodisrupt/laravel-agent
sierratecnologia/media-manager