Rania
Rania

Reputation: 3

Show nearest pharmacy to user's location when provided with longitude & latitude

I want to make an API using laraval which will have request of latitude and longitude and the response will provide json of location of user's nearest pharmacy
how can I do or simple example or tutorial similar ?

Upvotes: 0

Views: 131

Answers (1)

Almokhtar
Almokhtar

Reputation: 144

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();

Upvotes: 1

Related Questions