标签:
@link http://www.cnblogs.com/ycsfwhh/archive/2010/12/20/1911232.html
@link http://blog.csdn.net/b_h_l/article/details/8657040
这里讲的很具体,说实话菜鸟没看到。
下面只是我用php抄的,自己做下笔记。
class LantitudeLongitudeDist{ const EARTH_RADIUS = 6378137;//赤道半径(单位m) /** * 转化为弧度(rad) * */ private static function rad($d) { return (float)($d * pi() / 180.0); } /** * 基于余弦定理求两经纬度距离 * @param lon1 第一点的经度 * @param lat1 第一点的纬度 * @param lon2 第二点的经度 * @param lat3 第二点的纬度 * @return 返回的距离,单位m * */ public static function dist($lon1, $lat1, $lon2, $lat2) { $radLat1 = self::rad($lat1); $radLat2 = self::rad($lat2); $radLon1 = self::rad($lon1); $radLon2 = self::rad($lon2); if ($radLat1 < 0) $radLat1 = pi() / 2 + abs($radLat1);// south if ($radLat1 > 0) $radLat1 = pi() / 2 - abs($radLat1);// north if ($radLon1 < 0) $radLon1 = pi() * 2 - abs($radLon1);// west if ($radLat2 < 0) $radLat2 = pi() / 2 + abs($radLat2);// south if ($radLat2 > 0) $radLat2 = pi() / 2 - abs($radLat2);// north if ($radLon2 < 0) $radLon2 = pi() * 2 - abs($radLon2);// west $x1 = self::EARTH_RADIUS * cos($radLon1) * sin($radLat1); $y1 = self::EARTH_RADIUS * sin($radLon1) * sin($radLat1); $z1 = self::EARTH_RADIUS * cos($radLat1); $x2 = self::EARTH_RADIUS * cos($radLon2) * sin($radLat2); $y2 = self::EARTH_RADIUS * sin($radLon2) * sin($radLat2); $z2 = self::EARTH_RADIUS * cos($radLat2); $d = sqrt(($x1 - $x2) * ($x1 - $x2) + ($y1 - $y2) * ($y1 - $y2)+ ($z1 - $z2) * ($z1 - $z2)); //余弦定理求夹角 $theta = acos((self::EARTH_RADIUS * self::EARTH_RADIUS + self::EARTH_RADIUS * self::EARTH_RADIUS - $d * $d) / (2 * self::EARTH_RADIUS * self::EARTH_RADIUS)); $dist = $theta * self::EARTH_RADIUS; return $dist; } } $r = LantitudeLongitudeDist::dist(‘116.4609990000‘, ‘39.9272240000‘, ‘116.4685450000‘, ‘39.9278320000‘); echo $r; exit();
标签:
原文地址:http://www.cnblogs.com/iamdoufu/p/4709196.html