Find nearby locations using the Haversine formula in Eloquent query
Uses the haversine formula to find all DB records with a location within a specified radius
Last updated
Was this helpful?
Uses the haversine formula to find all DB records with a location within a specified radius
Last updated
Was this helpful?
The haversine formula determines the between two points on a sphere given their and . Important in navigation, it is a special case of a more general formula in spherical trigonometry, the law of haversines, that relates the sides and angles of spherical triangles. (Wikipedia)
Personally, I have always found it easier to handle and store coordinates as strings. Very rarely is it required to perform any arithmetic operations on the values.
Add the following to an existing query builder instance, and then not forgetting to call get()
or paginate()
The query assumes that your table contains columns called latitude
and longitude
. You may need to adapt these to suit your use case.
References:
You can discuss pages on this site at