How to find nearest location using latitude and longitude in laravel
In this post we will give you information about How to find nearest location using latitude and longitude in laravel. Hear we will give you detail about How to find nearest location using latitude and longitude in laravelAnd how to use it also give you demo for it if it is necessary.
In this post, i would like to fine neareby place using latitude and longitude query in laravel eloquent. you can simply get nearest geolocation by mysql radius query in laravel 5.
If you have places, hotels, home, city, state, country, products, dealer, supplier etc with latitude and longitude location then you can simply find easily nearby you. so let’s see following sq query that can simply use in laravel model query.
Here is you can see sql query:
6371 * acos(cos(radians(" . $lat . "))
* cos(radians(posts.lat))
* cos(radians(posts.lon) - radians(" . $lon . "))
+ sin(radians(" .$lat. "))
* sin(radians(posts.lat))) AS distance
Example:
$lat = YOUR_CURRENT_LATTITUDE;
$lon = YOUR_CURRENT_LONGITUDE;
DB::table("posts")
->select("posts.id"
,DB::raw("6371 * acos(cos(radians(" . $lat . "))
* cos(radians(posts.lat))
* cos(radians(posts.lon) - radians(" . $lon . "))
+ sin(radians(" .$lat. "))
* sin(radians(posts.lat))) AS distance"))
->groupBy("posts.id")
->get();
I hope it can help you…
Hope this code and post will helped you for implement How to find nearest location using latitude and longitude in laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs