# Find nearby locations using the Haversine formula in Eloquent query

## Haversine

The **haversine formula** determines the [great-circle distance](https://en.wikipedia.org/wiki/Great-circle_distance) between two points on a sphere given their [longitudes](https://en.wikipedia.org/wiki/Longitude) and [latitudes](https://en.wikipedia.org/wiki/Latitude). 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)

### Managing coordinates

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.

{% hint style="info" %}
If your requirements are more complex, and you have mysql 8, consider Spatial queries instead. <https://dev.mysql.com/doc/refman/8.0/en/spatial-types.html>
{% endhint %}

### Eloquent or DB Query Builder

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.

```sql
// the centre of your search
$latitude = '56.32124';
$longitude = '-1.342934';

// search radius
$distance = 5;  //(miles - see note)

$query->selectRaw('(3959 * acos (
       cos ( radians(?) )
       * cos( radians( latitude ) )
       * cos( radians( longitude ) - radians(?) )
       + sin ( radians(?) )
       * sin( radians( latitude )))) AS distance',[
           $latitude,
           $longitude,
           $latitude
  ]);
            
  $query->havingRaw('distance <= ? OR 0', [$distance]);
```

{% hint style="info" %}
&#x20;The above assumes that you have distance in miles.  If distance is in KM then replace 3959 with 6371.
{% endhint %}

References:

* <https://stackoverflow.com/questions/574691/mysql-great-circle-distance-haversine-formula>
* <https://en.wikipedia.org/wiki/Haversine_formula>
*

{% hint style="info" %}
**You can discuss pages on this site at** [**https://github.com/snapey/talltips/discussions**](https://github.com/snapey/talltips/discussions)
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://talltips.novate.co.uk/laravel/find-nearby-locations-using-the-haversine-formula-in-eloquent-query.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
