New: Learn Statamic on Laracasts!

We've retired the forum in favor of GitHub Discussions.

New conversations can be started on GitHub. Existing conversations will remain for a while longer.

Head over to GitHub →

Filtering a collection by it's geolocation to get the nearest results.

Matthew Dixon July 16, 2019 by Matthew Dixon

Hi all, new to Statamic but hoping it can have a big impact on the way we work.

So I have a project I have to move into Statamic. In this project is a list of locations (300 or so) which all have latitude and longitude data in them.

As part of the website there is a google map with these planted on but the ability to search by the closest to your location.

So you could use Laravel and JSON database fields as so:

Location::selectRow("*", "(3959 * acos(cos(radians(json_unquote(json_extract(geocode, '$.\"lat\"'))))
    * cos(radians(" . $lat . "))
    * cos(radians(" . $lng . ")
    - radians(json_unquote(json_extract(address, '$.\"lng\"'))))
    + sin(radians(json_unquote(json_extract(address, '$."lat\"'))))
    * sin(radians(" . $lat . ")))) AS distance")

Obviously now without having a database I would need to interrogate the filesystem and do this programatically.

Using Statamic how can I load up the data and then sort it in such a way?

Do I use a Laravel approach of getting the items into a collection and then mapping them etc?

Just concerned if you had a few thousand records this could be pretty slow.

I just need some pointers in the right direction on how I can move forward with this.

Thanks all!

Answered by Erin Dalzell!
>>>>>>> Answered <<<<<<<
1 Reply