0

I need to do a new task in codeigniter , i have restaurant details having city, state ,country and postal code and now if in the front end if the user enter the city or postal code and he have a option to select km (kilometer) . and i need to show the restaurant avilable around within the kilometer what is available in my db . for this what can i do is there any script i need to use or any api need to use . is it possible ?

Anyone know if this can be accomplish? As I desperately need some help. Thanks in advance!

saravanan
  • 401
  • 1
  • 4
  • 11

1 Answers1

2

you need first to store latitude and longitude of all your restaurants then you can easly get near restaurants with somenthing like this in your model:

function getNear($lat =false,$lng=false,$distance = false /*km range*/){

$multiplier = 112.12; // use 69.0467669 if you want miles

$query = $this->db->query("SELECT *, (SQRT(POW((restaurants.lat - $lat), 2) + POW((restaurants.long - $lng), 2)) * $multiplier) AS distance FROM restaurants WHERE POW((restaurants.lat - $lat), 2) + POW((restaurants.long - $lng), 2) <= POW(($distance / $multiplier), 2)  ORDER BY distance ASC ;");

return $query->result();

}

then call it

$this->model->getNear($lat,$long,$distance_in_kilometers);

this query runs over a table called "restaurants" having fields "lat" and "long" containing places coordinates

itsme
  • 48,972
  • 96
  • 224
  • 345