/** * This function takes in objects of class gpsPoint where both latitude and * longitude are already set and calculates distance in metres between them * * @param \exercises\gps\entities\gpsPoint $gpsPoint1 * @param \exercises\gps\entities\gpsPoint $gpsPoint2 * @return float , distance rounded to metres * @throws \Exception */ public static function getDistanceBetweenTwoPoints(gpsPoint $gpsPoint1, gpsPoint $gpsPoint2) { $latitudePointA = deg2rad($gpsPoint1->getLatitude()); $longitudePointA = deg2rad($gpsPoint1->getLongitude()); $latitudePointB = deg2rad($gpsPoint2->getLatitude()); $longitudePointB = deg2rad($gpsPoint2->getLongitude()); $longitudeDelta = $longitudePointA - $longitudePointB; $haversineFormula = atan2(sqrt(pow(cos($latitudePointB) * sin($longitudeDelta), 2) + pow(cos($latitudePointA) * sin($latitudePointB) - sin($latitudePointA) * cos($latitudePointB) * cos($longitudeDelta), 2)), sin($latitudePointA) * sin($latitudePointB) + cos($latitudePointA) * cos($latitudePointB) * cos($longitudeDelta)); $arcDistanceInMetres = $haversineFormula * self::radiusOfEarthInMetres; return round($arcDistanceInMetres); }
/** * * Uses the customers returned array from getCustomerDataFromJSONFile. The method * checks each customer against the gpsPointA and allowed distance to filter out * customers living within a specified distance. * * @return array * @throws \Exception */ public function getCustomersWithinASpecifiedDistanceFromPointA() { if ($this->gpsPointA == NULL) { throw new \Exception("Please set the coordinates of Point A first."); } elseif ($this->distance == NULL) { throw new \Exception("Please enter the distance first."); } $customers = $this->getCustomerDataFromJSONFile(); $filteredCustomers = array(); foreach ($customers as $key => $customer) { $gpsPointB = new gpsPoint(); $gpsPointB->setLatitude($customer['latitude']); $gpsPointB->setLongitude($customer['longitude']); $calculatedDistanceBetweenPointBAndA = distanceBetweenTwoGPSPoints::getDistanceBetweenTwoPoints($this->gpsPointA, $gpsPointB); if ($calculatedDistanceBetweenPointBAndA <= $this->distance) { $filteredCustomers[$key]['name'] = $customer['name']; $filteredCustomers[$key]['distance'] = $calculatedDistanceBetweenPointBAndA; } unset($calculatedDistanceBetweenPointBAndA); unset($gpsPointB); } return $filteredCustomers; }