public function getUserInZoneFromArray($users, $current)
 {
     $toRtn = array();
     $i = 0;
     foreach ($users as $user) {
         if ($user != $current) {
             $userGeo = GeoLocation::fromDegrees(str_replace(',', '.', $user->getLatitude()), str_replace(',', '.', $user->getLongitude()));
             /** ===== distance : 1km ===== */
             if ($this->isInZone($userGeo, 1)) {
                 $toRtn[$i]['id'] = $user->getId();
                 $toRtn[$i]['username'] = $user->getUsername();
                 $i++;
             }
         }
     }
     return $toRtn;
 }
示例#2
0
include_once 'transporter.php';
include_once 'geolocation.php';
if (!isset($_POST['userid']) || !isset($_POST['lat']) || !isset($_POST['long']) || !isset($_POST['distance'])) {
    return;
}
//$_POST['userid'] = '1';
//$_POST['lat'] = '40.5187154';
//$_POST['long'] = '-74.4120953';
//$_POST['distance'] = '100';
$userId = $_POST['userid'];
$gpslat = $_POST['lat'];
$gpslong = $_POST['long'];
$distance = $_POST['distance'] / 3280.84;
//Convert ft to km
$edison = GeoLocation::fromDegrees($gpslat, $gpslong);
// get bounding coordinates 40 kilometers from location;
$coordinates = $edison->boundingCoordinates($distance, 6371.01);
//	print_r($coordinates);
$south = $coordinates[0]->degLat . " \n";
//South
$west = $coordinates[0]->degLon . " \n";
//West
$north = $coordinates[1]->degLat . " \n";
//North
$east = $coordinates[1]->degLon . " \n";
//East
$transporter = new Transporter();
$conn = $transporter->getConnection();
// execute the stored procedure
$sql = "CALL set_gps('{$userId}', '{$north}', '{$east}', '{$south}', '{$west}');";
 public function getPresentersByRadius()
 {
     $units = isset($this->data['units']) ? $this->data['units'] : "miles";
     if (!empty($this->data['zipcode'])) {
         //country on this next line defaults to us so we will need to pass the country.
         $lat = $this->data['lat'];
         $lng = $this->data['lng'];
         $radius = !empty($this->data['radius']) ? $this->data['radius'] : 25;
     } else {
         $zipcode = $this->Session->read('user_zip');
         if ($this->Session->check('user_lat') && $this->Session->read('user_lat') != 0) {
             $lat = $this->Session->read('user_lat');
         } else {
             $lat = $this->data['lat'];
             //$this->Session->write('user_lat', $lat);
         }
         if ($this->Session->check('user_lng') && $this->Session->read('user_lng') != 0) {
             $lng = $this->Session->read('user_lng');
         } else {
             $lng = $this->data['lng'];
             //$this->Session->write('user_lng', $lng);
         }
         $radius = 50;
     }
     set_include_path(get_include_path() . PATH_SEPARATOR . APP . "Vendor");
     require_once 'GeoLocation.php';
     $my_location = GeoLocation::fromDegrees($lat, $lng);
     if (isset($this->data['is_mobile'])) {
         $coordinates = $my_location->boundingCoordinates($radius, $units);
         $this->AddressGeocode->setDatasource('replicated');
         $locations = $this->AddressGeocode->find('all', array('fields' => array('Presenter.id', 'AddressGeocode.location_string', 'User.first_name', 'User.last_name', 'AddressGeocode.lat', 'AddressGeocode.lng'), 'group' => 'AddressGeocode.address_id', 'joins' => array(array('table' => 'addresses', 'alias' => 'Address', 'type' => 'INNER', 'conditions' => array('AddressGeocode.address_id=Address.id')), array('table' => 'presenters', 'alias' => 'Presenter', 'type' => 'INNER', 'conditions' => array('Address.user_id=Presenter.user_id')), array('table' => 'users', 'alias' => 'User', 'type' => 'INNER', 'conditions' => array('Presenter.user_id=User.id'))), 'conditions' => array("AND" => array(array("AddressGeocode.lat >= {$coordinates[0]->getLatitudeInDegrees()}", "AddressGeocode.lat <= {$coordinates[1]->getLatitudeInDegrees()}"), array("AddressGeocode.lng >= {$coordinates[0]->getLongitudeInDegrees()}", "AddressGeocode.lng <= {$coordinates[1]->getLongitudeInDegrees()}")), 'Address.address_type_id' => 1, 'Presenter.presenter_status_id' => 3)));
     } else {
         $locations = $this->AddressGeocode->presenterLocations($lat, $lng, $radius);
     }
     $local_users = array();
     foreach ($locations as $user) {
         $add = true;
         $presenter_types = $this->PresenterType->find('first', array('conditions' => array('PresenterType.presenter_id' => $user['Presenter']['id']), 'order' => 'presentertypes_id DESC'));
         switch ($presenter_types['PresenterType']['presentertypes_id']) {
             case '2':
                 $status = 'yellow';
                 break;
             case '3':
                 $status = 'pink';
                 break;
             case '4':
                 $status = 'blue';
                 break;
             case '5':
                 $status = 'green';
                 break;
             case '6':
                 $status = 'orange';
                 break;
             case '7':
                 $status = 'purple';
                 break;
             case '8':
                 $status = 'black';
                 break;
             case '9':
                 $status = 'black_lv1';
                 break;
             case '10':
                 $status = 'black_lv2';
                 break;
             case '11':
                 $status = 'black_lv3';
                 break;
             default:
                 $status = 'white';
                 break;
         }
         if ($this->data['green'] == 'true' && $presenter_types['PresenterType']['presentertypes_id'] < 5) {
             $add = false;
         }
         //get distance
         $their_location = GeoLocation::fromDegrees($user['AddressGeocode']['lat'], $user['AddressGeocode']['lng']);
         $distance = $my_location->distanceTo($their_location, $units);
         if ($distance > $radius) {
             $add = FALSE;
         }
         if ($add === true) {
             $location = explode(',', $user['AddressGeocode']['location_string']);
             $local_users[] = array('name' => isset($this->data['is_mobile']) ? ucwords(strtolower($user['User']['first_name'] . ' ' . $user['User']['last_name'])) : '', 'location' => $location[0], 'distance' => round($distance, 2), 'presenter_type' => $status, 'presentertype_id' => $presenter_types['PresenterType']['presentertypes_id'], 'lat' => $user['AddressGeocode']['lat'], 'lng' => $user['AddressGeocode']['lng'], 'presenter_id' => $user['Presenter']['id'], 'id' => $user['Presenter']['id']);
         }
     }
     usort($local_users, function ($a, $b) {
         if ($a['distance'] == $b['distance']) {
             return 0;
         }
         return $a['distance'] < $b['distance'] ? -1 : 1;
     });
     $this->sendSuccess($local_users);
 }