# 坐标 多种数据库计算GPS经纬度坐标距离

mysql计算GPS经纬度距离

DELIMITER 
CREATEDEFINER=root@localhost FUNCTIONgetDistance(
lon1 float(10,7)
,lat1 float(10,7)
,lon2 float(10,7)
,lat2 float(10,7)
) RETURNSdouble
begin
declared double;
setradius = 6378140; #假设地球为正球形，直径为6378140米
setd = (2*ATAN2(SQRT(SIN((lat1-lat2)*PI()/180/2)
*SIN((lat1-lat2)*PI()/180/2)+
COS(lat2*PI()/180)*COS(lat1*PI()/180)
*SIN((lon1-lon2)*PI()/180/2)
*SIN((lon1-lon2)*PI()/180/2)),
SQRT(1-SIN((lat1-lat2)*PI()/180/2)
*SIN((lat1-lat2)*PI()/180/2)
+COS(lat2*PI()/180)*COS(lat1*PI()/180)
*SIN((lon1-lon2)*PI()/180/2)
returnd;
end

sqlServer计算GPS经纬度距离

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[getDistance](@lng1 FLOAT,@lat1 FLOAT,@lng2 FLOAT,@lat2 FLOAT)
RETURNS FLOAT
AS
BEGIN
DECLARE @Distance      FLOAT
SET @EARTH_RADIUS = 6378140
SET @RadLatBegin = @lat1 *PI()/ 180.0
SET @RadLatEnd = @lat2 *PI()/ 180.0
SET @RadLngDiff = @lng1 *PI()/ 180.0 - @lng2 *PI()/ 180.0
SET @Distance = 2 *ASIN(
SQRT(
*POWER(SIN(@RadLngDiff / 2), 2)
)
)
SET @Distance =ROUND(@Distance * @EARTH_RADIUS,1)
--SET @Distance = Round(@Distance * 10000) / 10000
RETURN @Distance
END

Oracle计算GPS经纬度距离

create or replace FUNCTION GET_DISTANCE
(
LNG1 IN NUMBER,
LAT1 IN NUMBER,
LNG2 IN NUMBER,
LAT2 IN NUMBER
) RETURN NUMBER IS
DISTANCE NUMBER:=0;
PI NUMBER:=3.141592625;
BEGIN
RETURN DISTANCE;
END GET_DISTANCE;

PHP计算GPS经纬度坐标

<?php
functionrad($d){ return$d* 3.1415926535898 / 180.0;
}
functionGetDistance($lat1,$lng1, $lat2,$lng2){
$EARTH_RADIUS= 6378.137;$radLat1= rad($lat1);$radLat2= rad($lat2);$a= $radLat1-$radLat2;
$b= rad($lng1) - rad($lng2);$s= 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)));
$s=$s*$EARTH_RADIUS;$s= round($s* 10000) / 10000; return$s;
}
echoGetDistance(116.480733,39.98684,117.480733,39.98684);
?>