$current_city = find_city_by_id('cities.csv', $city); } else { $current_city = find_city_by_name('cities.csv', $city, $state); } if (isset($current_city)) { $cityname = $current_city->name; $radius = Flight::request()->query->radius; $nearby = find_distance($state, $cityname, $radius, 'cities.csv'); Flight::json($nearby); } }); // Allow users to mark visits to a city Flight::route('POST /v1/users/@user/visits', function ($user) { $city = Flight::request()->data->name; $state = Flight::request()->data->state; $current_city = find_city_by_name('cities.csv', $city, $state); if (isset($current_city)) { $city_id = $current_city->id; write_to_visits($user, $city_id); } }); // Allows users to see visited cities and enter new visited cities as a query Flight::route('GET /v1/users/@user/visits', function ($user) { $current_user = find_user('users.csv', $user); if (isset($current_user)) { $user_id = $current_user->id; // While I was developing, I used a form for the ease of UI in order to send to the POST. // echo '<form action="/flight_challenge/v1/users/'.$user_id.'/visits" method="POST"> // City: <input type="text" name="name"><br> // State: <input type="text" name="state"><br> // <input type="submit" value="Submit">
function find_distance($start_state, $start_city, $radius, $cityfile) { $cities = json_decode(add_headers_to_json($cityfile)); $start_city = str_replace("-", " ", $start_city); $start_city = find_city_by_name($cityfile, $start_city, $start_state); $nearby_cities = array(); $lon1 = deg2rad($start_city->longitude); // convert from degrees to radians $lat1 = deg2rad($start_city->latitude); $earth_radius = 3959; //in miles foreach ($cities as $city) { $lon2 = deg2rad($city->longitude); $lat2 = deg2rad($city->latitude); //determine difference $latDelta = $lat1 - $lat2; $lonDelta = $lon1 - $lon2; //Vicenty formula $lonDelta = $lon1 - $lon2; $a = pow(cos($lat1) * sin($lonDelta), 2) + pow(cos($lat2) * sin($lat1) - sin($lat2) * cos($lat1) * cos($lonDelta), 2); $b = sin($lat2) * sin($lat1) + cos($lat2) * cos($lat1) * cos($lonDelta); $angle = atan2(sqrt($a), $b); $distance = $angle * $earth_radius; if ($distance <= $radius && $distance != 0) { // for UI // echo $city->name.", ".$city->state.': '.$distance.' miles away<br>'; $nearby_cities[] = $city; } } return $nearby_cities; }