Laravel Find Nearest Restaurants from certain GPS Latitude and Longitude


Laravel, PHP find nearest Restaurants and Items using GPS Coordinate

I am currently working on a Food Delivery Application, where my users can search for  restaurants that are nearby to their current location, usually within 500 Meter to 1 Kilo Meter radius which can be configured by my users.

I am getting the current GPS coordinate of users from their mobile and passing it to the rest api which is based on Laravel. Rest Api will return me all the restaurants within the selected radius and from their current Latitude and Longitude. 

I am sharing the function here which might help somebody who are on the same boat as me with such a similar requirements.

Query Builder Approach

/*
 * @param1 : pass current latitude of the driver
 * @param2 : pass current longitude of the driver
 * @param3: pass the radius in meter/kilometer/miles within how much distance you wanted to filter
*/
private function findNearestRestaurants($latitude, $longitude, $radius = 400)
{
    /*
     * using query builder approach, useful when you want to execute direct query
     * replace 6371000 with 6371 for kilometer and 3956 for miles
     */
    $restaurants = DB::table('restaurant_table')
                    ->selectRaw("id, name, address, latitude, longitude, rating, zone ,
                     ( 6371000 * acos( cos( radians(?) ) *
                       cos( radians( latitude ) )
                       * cos( radians( longitude ) - radians(?)
                       ) + sin( radians(?) ) *
                       sin( radians( latitude ) ) )
                     ) AS distance", [$latitude, $longitude, $latitude])
        ->where('active', '=', 1)
        ->having("distance", "<", $radius)
        ->orderBy("distance",'asc')
        ->offset(0)
        ->limit(20)
        ->get();

    return $restaurants;
}

Eloquent Model Approach 

/*
 * @param1 : pass current latitude of the driver
 * @param2 : pass current longitude of the driver
 * @param3: pass the radius in meter within how much distance you wanted to fiter
*/
private function findNearestRestaurants($latitude, $longitude, $radius = 400)
{
    /*
     * using eloquent approach, make sure to replace the "Restaurant" with your actual model name
     * replace 6371000 with 6371 for kilometer and 3956 for miles
     */
    $restaurants = Restaurant::selectRaw("id, name, address, latitude, longitude, rating, zone ,
                     ( 6371000 * acos( cos( radians(?) ) *
                       cos( radians( latitude ) )
                       * cos( radians( longitude ) - radians(?)
                       ) + sin( radians(?) ) *
                       sin( radians( latitude ) ) )
                     ) AS distance", [$latitude, $longitude, $latitude])
        ->where('active', '=', 1)
        ->having("distance", "<", $radius)
        ->orderBy("distance",'asc')
        ->offset(0)
        ->limit(20)
        ->get();

    return $restaurants;
}

Please write your comments bellow if you have any on "how to find closest objects by their GPS Longitude and Latitude from current Location Coordinate and certain Radius".

Written by Akram Wahid 1 month ago

are you looking for a chief cook who can well craft laravel and vuejs, to make some awsome butterscotch,
yes then it is right time for you to look at my profile.

Do you want to write Response or Comment?

You must be a member of techalyst to proceed!

Continue with your Email ? Sign up / log in

Responses

Be the first one to write a response :(

{{ item.member.name }} - {{ item.created_at_human_readable }}

{{ reply.member.name }} - {{ reply.created_at_human_readable }}