Calculates the distance between the given coordinate
and this coordinate.
public getDistance ( |
||
$coordinate | ||
$calculator | Location\Distance\DistanceInterface | instance of distance calculation class |
Результат | float |
function dist_($lat1, $lng1, $lat2, $lng2) { if ($this->is_lat($lat1) && $this->is_lng($lng1) && $this->is_lat($lat2) && $this->is_lng($lng2)) { $c1 = new Coordinate($lat1, $lng1); $c2 = new Coordinate($lat2, $lng2); return $c1->getDistance($c2, new Haversine()); } else { return PHP_INT_MAX; } }
/** * @SWG\Get( * path="/stun", * summary="Request for stun/turn time limited long term credential", * tags={"rest api"}, * produces={"application/json"}, * @SWG\Parameter( * description="username fragement, any desired application data", * in="query", * name="ufrag", * required=false, * type="string", * maximum=25 * ), * @SWG\Parameter( * description="realm, the domain of the shared secret, default=lab.vvc.niif.hu", * in="query", * name="realm", * required=false, * type="string", * maximum=254, * ), * @SWG\Parameter( * description="client browser IPv4/IPv6 Address", * in="query", * name="ip", * required=false, * type="string", * maximum=45 * ), * @SWG\Response( * response="200", * description="STUN time limited credentials", * @SWG\Schema( * ref="#/definitions/ApiResponse" * ) * ), * security={ * { * "api_key":{ } * } * } * ) */ public function Get() { $app = \Slim\Slim::getInstance(); //default realm $realm = "lab.vvc.niif.hu"; try { define("MAXSERVERS", 2); //connectdb $db = Db::Connection("coturn-rest"); /// TOKEN IS A PARAM VARIABLE $token = $app->request->params('api_key'); /// TOKEN IS A HEADER VARIABLE //$app->request->headers('api_key') //TODO validate token: $sth = $db->prepare("SELECT count(*) AS count FROM token where token='{$token}'"); $sth->execute(); $result = $sth->fetchAll(PDO::FETCH_ASSOC); if ($result[0]["count"] == 1) { // response $response = new ApiResponse(); $response->ttl = 86400; // if ufrag if (empty($app->request->params('ufrag'))) { $response->username = time() + $response->ttl; } else { $response->username = time() + $response->ttl . ":" . $app->request->params('ufrag'); } //update not existing lat long in server table $sth = $db->prepare("SELECT id,ip FROM ip where latitude IS NULL OR longitude IS NULL"); $sth->execute(); $result = $sth->fetchAll(PDO::FETCH_ASSOC); foreach ($result as $row => $columns) { $location = $this->GetGeoIP($columns['ip']); $sth2 = $db->prepare("UPDATE ip SET latitude={$location->latitude}, longitude={$location->longitude} WHERE id={$columns['id']}"); $sth2->execute(); } $uris = array(); //check if ip presents if ($app->request->params('ip') && ($location = $this->GetGeoIP($app->request->params('ip')))) { //geoip distance $client_coordinate = new Coordinate($location->latitude, $location->longitude); $sth = $db->prepare("SELECT id,latitude,longitude FROM ip"); $sth->execute(); $result = $sth->fetchAll(PDO::FETCH_ASSOC); foreach ($result as $row => $columns) { $server_coordinate = new Coordinate($columns['latitude'], $columns['longitude']); $ips[$columns['id']] = $client_coordinate->getDistance($server_coordinate, new Vincenty()); } asort($ips); $servers = array(); foreach ($ips as $id => $distance) { $sth = $db->prepare("SELECT server_id FROM ip WHERE id='" . $id . "'"); $sth->execute(); $result = $sth->fetchAll(PDO::FETCH_ASSOC); foreach ($result as $row => $columns) { array_push($servers, $columns["server_id"]); } } for ($i = 0; $i < MAXSERVERS; $i++) { //add turnserver $this->serverURIs($db, $servers[$i], $uris); } } else { //default random servers $sth = $db->prepare("SELECT id FROM server ORDER BY RAND() limit " . MAXSERVERS); $sth->execute(); $result = $sth->fetchAll(PDO::FETCH_ASSOC); foreach ($result as $row => $columns) { //add a turnserver $this->serverURIs($db, $columns["id"], $uris); } } //implode uris $response->uris = $uris; //check if realm presents if ($app->request->params('realm')) { $realm = $app->request->params('realm'); } $sth = $db->prepare("SELECT value FROM turn_secret where realm='{$realm}' ORDER BY timestamp DESC limit 1"); $sth->execute(); $sharedsecret = $sth->fetchColumn(); if ($sharedsecret) { $response->password = base64_encode(hash_hmac("sha1", $response->username, $sharedsecret, true)); $app->response->setStatus(200); $app->response()->headers->set('Content-Type', 'application/json'); echo json_encode($response); $db = null; } else { throw new PDOException('No records found.'); } } else { $app->response->setStatus(403); $app->response()->headers->set('Content-Type', 'application/json'); echo '{"error":{"text": "Invalid api_key" }}'; } } catch (PDOException $e) { $app->response()->setStatus(500); $app->response()->headers->set('Content-Type', 'application/json'); echo '{"error":{"text": "' . $e->getMessage() . '"}}'; } }
/** * @covers Location\Coordinate::getDistance */ public function testGetdistance() { $coordinate1 = new Coordinate(19.820664, -155.468066, $this->ellipsoid); $coordinate2 = new Coordinate(20.709722, -156.253333, $this->ellipsoid); $this->assertEquals(128130.85, $coordinate1->getDistance($coordinate2, new Vincenty())); }