日期:2014-05-16 浏览次数:20762 次
class Distance{ const EARTH_RADIUS = 6378.137; // earth radius (const) kilometer /** * * Example: * $precision = 49; * ini_set("precision", $precision); * echo pi(); //will output 3.141592653589793115997963468544185161590576171875 * * 1 kilometer = 0.621371192 mile * * @param double d * @return double data */ private function rad( $d ) { return $d * M_PI / 180.0; } /** * * Example : get_distance(44.2112,-88.4175,34.5082,-82.6498) * Get Distance between two point : longitude,latitude(lat1,lng1 => lat2,lng2 ) * * @param double d * @return double data * */ public function get_distance( $lat1, $lng1, $lat2, $lng2, $base = 1000 ) { $radLat1 = floatval( $this->rad($lat1) ); $radLat2 = floatval( $this->rad($lat2) ); $a = $radLat1 - $radLat2; $b = $this->rad($lng1) - $this->rad($lng2); $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2))); $s = $s * self::EARTH_RADIUS; $s = round($s * 10000) / 10000; return $s; } }