Save coordinates to database and then do a range search

2.7k Views Asked by At

I’m using the TomTom API to find the coordinates of some places and store them into a database. What I’m trying to do now is doing a range research on only those locations. Let’s say I input 10km of range and a place, I’d expect to see the list of places I’ve saved in my db that are inside that range. How can I do that? I’m using Laravel and Vue

1

There are 1 best solutions below

0
On BEST ANSWER

You can create a migration for your places and add these 2 columns for your lat and long:

        $table->decimal('latitude', 11, 8)->nullable();
        $table->decimal('longitude', 11, 8)->nullable();

After that you can store your locations inside your db.

Then you can use whereBetween condition in your query:

$locations = Location::whereBetween('latitude', [1, 100])->whereBetween('longitude', [200, 300])->get(); 

WhereBetween condition accepts 2 arguments, column name and min-max values respectively.