You could use this package. It would involve some mucking about with the database, but would save you from some geometry ...
Feb 27, 2018
18
Level 10
How to find nearest cities from Latitude and Longitude
Hello GUYS , I am working in a project that I want make a search with latitude and Logitude , like this -
latitude -21.1774558
longitude -47.8063571
and return all cities with RADIUS of 30 km around this coordinate.
I have all the cities with the Longitude and Latitude in My DB. like this -
name_city | latitude | longitude
Can I calculate those values and get all the cities around or do I have to use some IPI from Google ?
Level 28
If you already have the latitude and longitudes in your database you might as well query them yourself. This is from an older project (so may not work or be the best method) where I'm doing something similar:
$cities = City::select(DB::raw('*, ( 6367 * acos( cos( radians('.$latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$longitude.') ) + sin( radians('.$latitude.') ) * sin( radians( latitude ) ) ) ) AS distance'))
->having('distance', '<', 25)
->orderBy('distance')
->get();
5 likes
Please or to participate in this conversation.