How to get nearby courts from a given lat in a laravel?

I want to get basketball courts based on a given lat long in an eloquent ORM. I tried to get this, but here I got this error.

Call to undefined function App\Http\Controllers\Api\RADIANS()

Is there a way to get the nearby location using Eloquent, if so how? please help me, I am sharing my cases so far.

HomeCourt Controller

public function fetchHomeCourts(Request $request) {

    $validator = Validator::make(
        array(
            'lat' => $request->lat,
            'long' => $request->long,
        ),
        array(
            'lat' => 'required',
            'long' => 'required',
        )
    );
    if ($validator->fails()) {
        $this->setMeta("422", Constant::MSG_422);
        return response()->json($this->setResponse());
    }
    $homeCourt= HomeCourt::where(ACOS(SIN(RADIANS('latitude'))*SIN(RADIANS($request->lat))+COS(RADIANS('latitude'))*COS(RADIANS($request->lat))*COS(RADIANS('longitude')-RADIANS($request->long)))*6380 < 10)->first();
    if(!$homeCourt) {
        $this->setMeta("200", "No courts found");
        $this->setData("homeCourts",$homeCourt);
        return response()->json($this->setResponse());
    }
    $this->setMeta("200", "Home court list");
    $this->setData("homeCourts",$homeCourt);
    return response()->json($this->setResponse());

}

      

+3


source to share


3 answers


So basically this happens:

The user will provide you with the coordinates they are interested in (through a magical procedure, most pages supply the map that users click on, but this is really what is best for your users).

This is what your code should read:

public function fetchHomeCourts(Request $request) {

    $validator = Validator::make(
        array(
            'lat' => $request->lat,
            'long' => $request->long,
        ),
        array(
            'lat' => 'required',
            'long' => 'required',
        )
    );
    if ($validator->fails()) {
        $this->setMeta("422", Constant::MSG_422);
        return response()->json($this->setResponse());
    }
    $homeCourt= HomeCourt::whereRaw("ACOS(SIN(RADIANS('latitude'))*SIN(RADIANS($request->lat))+COS(RADIANS('latitude'))*COS(RADIANS($request->lat))*COS(RADIANS('longitude')-RADIANS($request->long)))*6380 < 10")->first();
    if(!$homeCourt) {
        $this->setMeta("200", "No courts found");
        $this->setData("homeCourts",$homeCourt);
        return response()->json($this->setResponse());
    }
    $this->setMeta("200", "Home court list");
    $this->setData("homeCourts",$homeCourt);
    return response()->json($this->setResponse());

}

      



Bit whereRaw

will pass this whole formula to MySQL directly, instead of Laravel trying to figure out how to construct it in the query (since this is not a standard format that Laravel can handle.

From what I understand the formula is basically equal to the distance on the curved surface of the earth (6380 is the radius of the earth in km and the rest looks like the arc distance of two points).

Assuming all the courts you are interested in are stored in your database (you can either have the list manually or update them with some overnight process to get it from somewhere), you don't need to run an API request on every request.

+1


source


This can only be done with help Google Map Places Web Service API

, it will help you get the functionality you need.

Search by neighborhood allows you to search for places within a specific area. You can refine your search query by providing keywords or by specifying the type of place you are looking for.

A nearby lookup request is an HTTP URL of the following form:

https://maps.googleapis.com/maps/api/place/nearbysearch/output?parameters

      

where the output can be one of the following:



json (recommended) indicates output in JavaScript Object Notation (JSON)

xml indicates output as XML

      

Nearby search example

The following example is a search query for places like "restaurant" within 500 meters of a point in Sydney, Australia that contains the word "cruise" :

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=restaurant&keyword=cruise&key=YOUR_API_KEY

      

Link

+1


source


I tried the above answer without success, but thanks to @apokryfos answer I did it, so this is my code, I put it in a raw DB query.

\DB::table("users")
     ->select("users.id", \DB::raw("6371 * acos(cos(radians(" . $this->lat . "))
     * cos(radians(users.latitude)) 
     * cos(radians(users.longitude) - radians(" . $this->lng . ")) 
     + sin(radians(" .$this->lat. ")) 
     * sin(radians(users.latitude))) AS distance"))
     ->having('distance', '<', $this->rad)
     ->first();

      

0


source







All Articles