Advertisement
selfchief

Untitled

Jul 15th, 2011
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.03 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Вычисление расстояния между двумя географическими точками
  4.  *
  5.  * @author  Григорьев Олег aka vasa_c
  6.  * @author  Волобуев Сергей aka selfchief
  7.  */
  8.  
  9. namespace Maps\Helpers;
  10.  
  11. class Point
  12. {
  13.     const PI = 3.1415926;
  14.     const EARTH_RADIUS = 6372795;
  15.  
  16.     /**
  17.      * Конструктор
  18.      *
  19.      * @param float $x
  20.      *        долгота
  21.      * @param float $y
  22.      *        широта
  23.      */
  24.     public function __construct($x, $y) {
  25.         $this->x = $x;
  26.         $this->y = $y;
  27.     }
  28.  
  29.     /**
  30.      * Вычислить расстояние до другой точки
  31.      *
  32.      * @param Point|float $x
  33.      *        вторая точка или её долгота
  34.      * @param float $y [optional]
  35.      *        широта
  36.      * @return float
  37.      *         расстояние в метрах
  38.      */
  39.     public function distance($x, $y = null) {
  40.         if ($x instanceof self) {
  41.             $y = $x->getY();
  42.             $x = $x->getX();
  43.         }
  44.         $long1  = $this->x * self::PI / 180;
  45.         $lat1 = $this->y * self::PI / 180;
  46.         $long2  = $x * self::PI / 180;
  47.         $lat2 = $y * self::PI / 180;
  48.         $y = \sqrt(\pow(\cos($lat2) * \sin($long2 - $long1), 2) +
  49.              \pow(\cos($lat1) * \sin($lat2) - \sin($lat1) * \cos($lat2) * \cos($long2 - $long1), 2));
  50.         $x = \sin($lat1) * \sin($lat2) + \cos($lat1) * \cos($lat2) * \cos($long2 - $long1);
  51.         return \atan2($y, $x) * self::EARTH_RADIUS;
  52.     }
  53.  
  54.     /**
  55.      * Получить долготу
  56.      *
  57.      * @return float
  58.      */
  59.     public function getX() {
  60.         return $this->x;
  61.     }
  62.  
  63.     /**
  64.      * Получить широту
  65.      *
  66.      * @return float
  67.      */
  68.     public function getY() {
  69.         return $this->y;
  70.     }
  71.  
  72.     /**
  73.      * Долгота
  74.      *
  75.      * @var float
  76.      */
  77.     private $x;
  78.  
  79.     /**
  80.      * Широта
  81.      *
  82.      * @var float
  83.      */
  84.     private $y;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement