PHP计算两个GPS点之间的距离
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<pre class="inline:true class:language-php decode:1 " > #lng为经度,lat为纬度,一定不要弄错了哦 function distance($lat1, $lng1, $lat2, $lng2){ return (2*ATAN2(SQRT(SIN(($lat1-$lat2)*PI()/180/2) *SIN(($lat1-$lat2)*PI()/180/2)+ COS($lat2*PI()/180)*COS($lat1*PI()/180) *SIN(($lng1-$lng2)*PI()/180/2) *SIN(($lng1-$lng2)*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(($lng1-$lng2)*PI()/180/2) *SIN(($lng1-$lng2)*PI()/180/2))))*6378140; } |
调用方法:
1 |
<pre class="inline:true class:language-php decode:1 " >echo distance(39.91917,116.3896,39.91726,116.3940); |
Mysql计算两GPS坐标的距离
1 2 3 |
<pre class="inline:true class:language-sql decode:1 " >drop function getDistance; DELIMITER $$ CREATE DEFINER=<pre class="inline:true decode:1 " >root |
@
localhost FUNCTION
getDistance(
lng1 float(10,7)
,lat1 float(10,7)
,lng2 float(10,7)
,lat2 float(10,7)
) RETURNS double
begin
declare d double;
declare radius int;
set radius = 6378140; #假设地球为正球形,直径为6378140米
set d = (2*ATAN2(SQRT(SIN((lat1-lat2)*PI()/180/2)
*SIN((lat1-lat2)*PI()/180/2)+
COS(lat2*PI()/180)*COS(lat1*PI()/180)
*SIN((lng1-lng2)*PI()/180/2)
*SIN((lng1-lng2)*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((lng1-lng2)*PI()/180/2)
*SIN((lng1-lng2)*PI()/180/2))))*radius;
return d;
end
$$
DELIMITER ;
select getDistance(116.3899,39.91578,116.3904,39.91576); #调用函数
Mysql计算两GPS坐标的距离SQL语句:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<pre class="inline:true class:language-sql decode:1 " >#lat为纬度, lng为经度, 一定不要弄错 declare @lng1 float; declare @lat1 float; declare @lng2 float; declare @lat2 float; set @lng1=116.3899; set @lat1=39.91578; set @lng2=116.3904; set @lat2=39.91576; select (2*ATAN2(SQRT(SIN((@lat1-@lat2)*PI()/180/2) *SIN((@lat1-@lat2)*PI()/180/2)+ COS(@lat2*PI()/180)*COS(@lat1*PI()/180) *SIN((@lng1-@lng2)*PI()/180/2) *SIN((@lng1-@lng2)*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((@lng1-@lng2)*PI()/180/2) *SIN((@lng1-@lng2)*PI()/180/2))))*6378140; |
错误及解决方法:
在创建函数前报如下错误:
1 |
This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) |
解决方法:
在创建函数前执行下面SQL语句:
1 2 |
set global log_bin_trust_function_creators=1; |
或者修改my.cnf文件并重启mysqld服务:
1 |
log_bin_trust_function_creators=1 |
Javascript计算两个GPS坐标的距离
1 2 3 4 5 6 7 8 9 10 11 |
function toRad(d) { return d * Math.PI / 180; } function getDisance(lat1, lng1, lat2, lng2) { #lat为纬度, lng为经度, 一定不要弄错 var dis = 0; var radLat1 = toRad(lat1); var radLat2 = toRad(lat2); var deltaLat = radLat1 - radLat2; var deltaLng = toRad(lng1) - toRad(lng2); var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2))); return dis * 6378137; } alert( getDisance(39.91917,116.3896,39.91726,116.3940) ); |
浏览量: 11