public function __construct($geoPoint = null, $address = null, $encodedLocation = null, $GeoLocationType = null) { parent::__construct(); $this->geoPoint = $geoPoint; $this->address = $address; $this->encodedLocation = $encodedLocation; $this->GeoLocationType = $GeoLocationType; }
public function getIpLocation($address) { $address = trim($address); // lets look in our database first. $location = GeoLocation::getByAddress($address, $this->database); if ($location != null) { // touch cache timer $location->save(); return $location->getData(); } // OK, it's not there, let's do an IP2Location lookup. $result = $this->getResult($address); if ($result != null) { $location = new GeoLocation(); $location->setDatabase($this->database); $location->setAddress($address); $location->setData($result); $location->save(); return $result; } return null; }
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; }
public function __construct($geoPoint = NULL, $address = NULL, $encodedLocation = NULL, $GeoLocationType = NULL) { if (get_parent_class('InvalidGeoLocation')) { parent::__construct(); } $this->geoPoint = $geoPoint; $this->address = $address; $this->encodedLocation = $encodedLocation; $this->GeoLocationType = $GeoLocationType; }
<?php session_start(); /** * отладка класса GeoLocation - */ ini_set('display_errors', 1); error_reporting(E_ALL ^ E_NOTICE); header('Content-type: text/html; charset=utf-8'); include_once __DIR__ . '/local.php'; include_once __DIR__ . '/request_url.php'; mb_internal_encoding("UTF-8"); // загружаем параметры---// $answ = ['successful' => false, 'message' => 'ERROR:тип запроса не распознан "' . $operation . '"']; $geo = new GeoLocation(); $lat = -1; // 51.7727 ; // $taskPar->getParameter('lat') ; $long = -1; // 55.0988 ; $geo->selectCity($lat, $long); $answ = $geo->getResult(); var_dump($answ);
<?php require "geo_location.php"; if (isset($_GET["ip"])) { try { $location = GeoLocation::find($_GET["ip"]); } catch (Exception $ex) { $message = "Failed with message: " . $ex->getMessage(); } } ?> <html> <head> <title>Geo location</title> </head> <body onload=""> <h2>IP address results:</h2> <?php echo "Country: " . $location->getCountryName(); echo "<br/>"; echo "Region: " . $location->getRegionName(); echo "<br/>"; echo "City: " . $location->getCity(); echo "<br/>"; ?> </body> </html>
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}');";
/** * <p>Computes the bounding coordinates of all points on the surface * of a sphere that have a great circle distance to the point represented * by this GeoLocation instance that is less or equal to the distance * argument.</p> * <p>For more information about the formulae used in this method visit * <a href="http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates"> * http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates</a>.</p> * * @param double $distance the distance from the point represented by this * GeoLocation instance. Must me measured in the same unit as the radius * argument. * @param string $unit_of_measurement * @throws \Exception * @internal param radius the radius of the sphere, e.g. the average radius for a * spherical approximation of the figure of the Earth is approximately * 6371.01 kilometers. * @return GeoLocation[] an array of two GeoLocation objects such that:<ul> * <li>The latitude of any point within the specified distance is greater * or equal to the latitude of the first array element and smaller or * equal to the latitude of the second array element.</li> * <li>If the longitude of the first array element is smaller or equal to * the longitude of the second element, then * the longitude of any point within the specified distance is greater * or equal to the longitude of the first array element and smaller or * equal to the longitude of the second array element.</li> * <li>If the longitude of the first array element is greater than the * longitude of the second element (this is the case if the 180th * meridian is within the distance), then * the longitude of any point within the specified distance is greater * or equal to the longitude of the first array element * <strong>or</strong> smaller or equal to the longitude of the second * array element.</li> * </ul> */ public function boundingCoordinates($distance, $unit_of_measurement) { $radius = $this->getEarthsRadius($unit_of_measurement); if ($radius < 0 || $distance < 0) throw new \Exception('Arguments must be greater than 0.'); // angular distance in radians on a great circle $this->angular = $distance / $radius; $minLat = $this->radLat - $this->angular; $maxLat = $this->radLat + $this->angular; $minLon = 0; $maxLon = 0; if ($minLat > self::$MIN_LAT && $maxLat < self::$MAX_LAT) { $deltaLon = asin(sin($this->angular) / cos($this->radLat)); $minLon = $this->radLon - $deltaLon; if ($minLon < self::$MIN_LON) $minLon += 2 * pi(); $maxLon = $this->radLon + $deltaLon; if ($maxLon > self::$MAX_LON) $maxLon -= 2 * pi(); } else { // a pole is within the distance $minLat = max($minLat, self::$MIN_LAT); $maxLat = min($maxLat, self::$MAX_LAT); $minLon = self::$MIN_LON; $maxLon = self::$MAX_LON; } return array( GeoLocation::fromRadians($minLat, $minLon), GeoLocation::fromRadians($maxLat, $maxLon) ); }
/** * * @param type $distance * @param type $unit_of_measurement * @return \Orb\Helpers\GeoLocation[] * @throws \Exception */ public function bounds($distance) { if ($distance < 0) { throw new \Exception('Arguments must be greater than 0.'); } // angular distance in radians on a great circle $this->angular = $distance / $this->radius(); $min_lat = $this->rad_lat - $this->angular; $max_lat = $this->rad_lat + $this->angular; $min_lon = 0; $max_lon = 0; if ($min_lat > $this->min_lat && $max_lat < $this->max_lat) { $deltaLon = asin(sin($this->angular) / cos($this->rad_lat)); $min_lon = $this->rad_lng - $deltaLon; if ($min_lon < $this->min_lng) { $min_lon += 2 * pi(); } $max_lon = $this->rad_lng + $deltaLon; if ($max_lon > $this->max_lng) { $max_lon -= 2 * pi(); } } else { // a pole is within the distance $min_lat = max($min_lat, $this->min_lat); $max_lat = min($max_lat, $this->max_lat); $min_lon = $this->min_lng; $max_lon = $this->max_lng; } return [GeoLocation::fromRad($min_lat, $min_lon), GeoLocation::fromRad($max_lat, $max_lon)]; }
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); }
/** * <p>Computes the bounding coordinates of all points on the surface * of a sphere that have a great circle distance to the point represented * by this GeoLocation instance that is less or equal to the distance * argument.</p> * <p>For more information about the formulae used in this method visit * <a href="http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates"> * http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates</a>.</p> * @param distance the distance from the point represented by this * GeoLocation instance. Must me measured in the same unit as the radius * argument. * @param radius the radius of the sphere, e.g. the average radius for a * spherical approximation of the figure of the Earth is approximately * 6371.01 kilometers. * @return an array of two GeoLocation objects such that:<ul> * <li>The latitude of any point within the specified distance is greater * or equal to the latitude of the first array element and smaller or * equal to the latitude of the second array element.</li> * <li>If the longitude of the first array element is smaller or equal to * the longitude of the second element, then * the longitude of any point within the specified distance is greater * or equal to the longitude of the first array element and smaller or * equal to the longitude of the second array element.</li> * <li>If the longitude of the first array element is greater than the * longitude of the second element (this is the case if the 180th * meridian is within the distance), then * the longitude of any point within the specified distance is greater * or equal to the longitude of the first array element * <strong>or</strong> smaller or equal to the longitude of the second * array element.</li> * </ul> */ public function boundingCoordinates($distance, $radius) { if ($radius < 0 || $distance < 0) { throw new Exception('Arguments must be greater than 0.'); } // angular distance in radians on a great circle $radDist = $distance / $radius; $minLat = $this->radLat - $radDist; $maxLat = $this->radLat + $radDist; $minLon = 0; $maxLon = 0; if ($minLat > MIN_LAT && $maxLat < MAX_LAT) { $deltaLon = asin(sin($radDist) / cos($this->radLat)); $minLon = $this->radLon - $deltaLon; if ($minLon < MIN_LON) { $minLon += 2 * pi(); } $maxLon = $this->radLon + $deltaLon; if ($maxLon > MAX_LON) { $maxLon -= 2 * pi(); } } else { // a pole is within the distance $minLat = max($minLat, MIN_LAT); $maxLat = min($maxLat, MAX_LAT); $minLon = MIN_LON; $maxLon = MAX_LON; } return array(GeoLocation::fromRadians($minLat, $minLon), GeoLocation::fromRadians($maxLat, $maxLon)); }