public function __construct()
{
// Scan and create WifiAccessPoints
$wifiAccessPoints = [];
if (SCAN_NEARBY_WIFI_ACCESS_POINTS === true) {
try {
$wifiAccessPoints = WifiAccessPoints::fromAccessPointScannerResponse(AccessPointScanner::scan());
$this->accessPointCount = count($wifiAccessPoints);
} catch (AccessPointScannerException $apse) {
$this->warnings[] = $apse->getMessage();
}
if (count($wifiAccessPoints) < 2) {
$this->warnings[] = "Less than two Wifi Access Points in proximity.";
}
}
// Geolocation
try {
$geolocationApi = new GeolocationAPI();
$geolocationResponse = $geolocationApi->call(GeolocationRequest::fromArrayDefinition([GeolocationRequest::DEFINITION_WIFI_ACCESS_POINTS => $wifiAccessPoints]));
$this->latitude = $geolocationResponse->getLatitude();
$this->longitude = $geolocationResponse->getLongitude();
$this->accuracy = $geolocationResponse->getAccuracy();
} catch (GeolocationResponseException $gre) {
$this->errors[] = "Geolocation API Error status {$gre->getStatusCode()}: {$gre->getMessage()}";
}
if ($this->latitude && $this->longitude) {
// distanceMatrix
try {
$distanceMatrixAPI = new DistanceMatrixAPI();
$distanceMatrixResponse = $distanceMatrixAPI->call(DistanceMatrixRequest::fromArrayDefinition([DistanceMatrixRequest::DEFINITION_ORIGINS => $this->latitude . ',' . $this->longitude, DistanceMatrixRequest::DEFINITION_DESTINATIONS => DESTINATION, DistanceMatrixRequest::DEFINITION_KEY => API_KEY, DistanceMatrixRequest::DEFINITION_LANGUAGE => LANGUAGE, DistanceMatrixRequest::DEFINITION_UNITS => UNITS]));
$this->durationInTraffic = $distanceMatrixResponse->getRows()->getDurationInTraffic();
$this->duration = $distanceMatrixResponse->getRows()->getDuration();
$this->distance = $distanceMatrixResponse->getRows()->getDistance();
$this->originAddress = $distanceMatrixResponse->getOriginAddresses();
$this->destinationAddress = $distanceMatrixResponse->getDestinationAddresses();
$this->computeDelay();
$this->computeGoogleMapsLink();
} catch (DistanceMatrixResponseException $e) {
$this->errors[] = "Distance Matrix API Error: " . $this->distanceMatrixErrorMessages[$e->getMessage()];
} catch (DistanceMatrixResponseElementException $e) {
$this->errors[] = "Distance Matrix API Error: " . $this->distanceMatrixElementErrorMessages[$e->getMessage()];
}
}
}