0

I have a Model called Place having a simple structure:

| id | name  | lat       | lon         | 
|----|-------|-----------|-------------|
| 1  | hotel | 33.755787 | -116.359998 |
| 2  | shop  | 44.968046 | -94.420307  |
| 3  | park  | 44.333304 | -91.493768  |

What I need is something like Place::whereRadius(100)->get() or some accurate latitude/longitude so that I can build a query like this:

Place::whereBetween('lat',latitudes)->whereBetween('lon',longitude)->get();

Currently my approach is to get lat/lon points of a circle of radius apart from my desired lat/lon point which I did via turf.js on frontend and got an array of lat/lon coordinates like this:

//example not real calculation
[
 [33.755787, -116.359998],
 [44.968046, -94.420307],
 [44.333304, -91.493768]
]

My next step was to calculate possible max and min points within the area of the above received circle but it's not promising accurate results with larger datasets or larger circles even in small datasets.

Is there an efficient way to query lat/lon based on radius & with some Eloquence if possible?

Edit I am looking for Laravel/Eloquent way of doing it. Raw MySQL based radius as suggested can technically work but isn't exactly what I asked for in the question

Metabolic
  • 2,808
  • 3
  • 27
  • 41
  • 1
    See: https://stackoverflow.com/questions/15372705/calculating-a-radius-with-longitude-and-latitude and https://en.wikipedia.org/wiki/Great-circle_distance – RyanNerd Oct 29 '20 at 00:20
  • @RyanNerd I am not asking for calculating a circle, that I can do, what I need is to fetch the points within that circle from DB – Metabolic Oct 29 '20 at 00:23
  • 1
    I worked this out in plain SQL, not Eloquent, here: https://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/ – O. Jones Oct 29 '20 at 01:16

0 Answers0