php - how to search all the location for given distance and given latitude and longitude in laravel -


this senario: saved shop details in mysql database in following way.

shopname, latitude, longitude,

now if gives location in latitude , longitude distance (like 5km) needs filler shops within 5km him,

in here user center , radius 5km , need find shop within circle, application developed in laravel 5.1

i tried follow code, wouldn't work.

can me.

you can add following code in model

public function scopedistance($query,$from_latitude,$from_longitude,$distance) {   // calculate distance in km   // if want in miles use 3959 instead of 6371   $raw = \db::raw('round ( ( 6371 * acos( cos( radians('.$from_latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$from_longitude.') ) + sin( radians('.$from_latitude.') ) * sin( radians( latitude ) ) ) ) ) distance');   return $query->select('*')->addselect($raw)->orderby( 'distance', 'asc' )->groupby('distance')->having('distance', '<=', $distance); } 

and can use this

$ads = ad::with(['user','photos'])->distance($from_latitude,$from_longitude,$distance)->get(); 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -