PHP code example of bagusindrayana / laravel-coordinate
1. Go to this page and download the library: Download bagusindrayana/laravel-coordinate library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bagusindrayana / laravel-coordinate example snippets
#use trait
use Bagusindrayana\LaravelCoordinate\Traits\LaravelCoordinate;
class Toko extends Model
{
use LaravelCoordinate;
//optional
public $_latitudeName = "latitude_column"; //default name is latitude
public $_longitudeName = "longitude_column"; //default name is longitude
//
}
//get data at a distance of 500 meters (0.5KM)
$tokos = Toko::nearby([
-0.497493,//latitude
117.156480//longitude
],0.5)->get();
//using order, remember that the order is always placed at the end of the query
//get data with a distance of 1 kilometer and sort it from farthest
$tokos = Toko::nearby([
-0.497493,//latitude
117.156480//longitude
],1)->farthest()->get();
//get data with a distance of 1 kilometer and sort it from closest to closest
$tokos = Toko::nearby([
-0.497493,//latitude
117.156480//longitude
],1)->closest()->get();
//add a custom column containing the distance value of each record
$tokos = Toko::nearby([
-0.497493,//latitude
117.156480//longitude
],0.5) //0.5 Km
->selectDistance(['id','nama_toko'],'_distance') //this function will add a custom column/alias with the name "_distance" which contains the distance value of each record
->get();
$tokos = Toko::nearby([
-0.497493,//latitude
117.156480//longitude
],
0.5,
1//using Spherical Law of Cosines
)
->get();