计算两GPS坐标的实际距离
javascript:
function toRadians(degree) { return degree * Math.PI / 180; } function distance(latitude1, longitude1, latitude2, longitude2) { // R is the radius of the earth in kilometers var R = 6371; var deltaLatitude = toRadians(latitude2-latitude1); var deltaLongitude = toRadians(longitude2-longitude1); latitude1 =toRadians(latitude1); latitude2 =toRadians(latitude2); var a = Math.sin(deltaLatitude/2) * Math.sin(deltaLatitude/2) + Math.cos(latitude1) * Math.cos(latitude2) * Math.sin(deltaLongitude/2) * Math.sin(deltaLongitude/2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = R * c; return d; }
php:
/** * 计算两会标之间的距离(米) * @param double $lng_a 经度a * @param double $lat_a 维度a * @param double $lng_b 经度b * @param double $lat_b 维度b */ function get_distance($lng_a, $lat_a, $lng_b, $lat_b) { //R是地球半径(米) $R = 6366000; $pk = doubleval(180 / 3.14169); $a1 = doubleval($lat_a / $pk); $a2 = doubleval($lng_a / $pk); $b1 = doubleval($lat_b / $pk); $b2 = doubleval($lng_b / $pk); $t1 = doubleval(cos($a1) * cos($a2) * cos($b1) * cos($b2)); $t2 = doubleval(cos($a1) * sin($a2) * cos($b1) * sin($b2)); $t3 = doubleval(sin($a1) * sin($b1)); $tt = doubleval(acos($t1 + $t2 + $t3)); return $R * $tt; }
sql:
已知坐标:(116.386294,39.923879)
地球半径:6366000米
SELECT id,Lastlongitude,Lastlatitude, (6366000 * ACOS( (COS(RADIANS(116.386042))*COS(RADIANS(39.923927))*COS(RADIANS(Lastlongitude))*COS(RADIANS(Lastlatitude)))+ (COS(RADIANS(39.923927))*SIN(RADIANS(116.386042))*COS(RADIANS(Lastlatitude))*SIN(RADIANS(Lastlongitude)))+ (SIN(RADIANS(39.923927))*SIN(RADIANS(Lastlatitude))) )) AS distance FROM drv_driver_location HAVING distance<10