#1.两点距离(1.4142135623730951)
select st_distance(point(0,0),point(1,1));
select st_distance(point (120.10591, 30.30163),point(120.13026,30.25961)) as distance HAVING distance>0 ORDER BY distance;
mysql 5.6 添加 #2.两点球面距离(157249.0357231545m)
select st_distance_sphere(point(0,0),point(1,1)) as distance HAVING distance>0 ORDER BY distance;
select st_distance_sphere(point (120.10591, 30.30163),point(120.13026,30.25961)) as distance HAVING distance>0 ORDER BY distance;
This function was added in MySQL 5.7.6.

第一个函数是计算平面坐标系下,两点的距离,就是

如果用于计算地球两点的距离,带入的参数是角度(经纬度),则计算的单位也是相差的角度,用此角度计算距离不准。纬度距离约111km每度,经度距离在赤道平面上是111km每度,随纬度的升高逐渐降低为0。

第二个函数是计算球面距离的公式,传入的参数是经纬度(经度-180~180,纬度-90~90),返回的值以m为单位的距离。

ST_Distance_Sphere(g1g2 [, radius])

如果mysql版本不支持上述函数怎么办?自己实现喽!下面是我自己写的球面距离函数

delimiter //
drop function if exists Spherical_Distance;
create function Spherical_Distance(jin1 double,wei1 double,jin2 double,wei2 double) returns double
NO SQL
BEGIN
declare j1 double;
declare w1 double;
declare j2 double;
declare w2 double;
declare R double;
set j1 = jin1*PI()/;
set w1 = wei1*PI()/;
set j2 = jin2*PI()/;
set w2 = wei2*PI()/;
set R = ;
return R*acos(cos(w1)*cos(w2)*cos(j1-j2)+sin(w1)*sin(w2));
END
//
delimiter ;

调用方式

select Spherical_Distance(120.10591,30.30163,120.13026,30.25961);

计算出的值和st_distance_sphere函数计算结果相差不大。

另外的求经纬度距离方法:

  $lat = trim($_POST['lat']);
$lng = trim($_POST['lng']);
$distance = "
ROUND(
6378.138 * * ASIN(
SQRT(
POW(
SIN(
(
'$lat' * PI() / - lat * PI() /
) /
), ) + COS(40.0497810000 * PI() / ) * COS(lat * PI() / ) * POW(
SIN(
(
'$lng' * PI() / - lng * PI() /
) /
), )
)
) * ) AS distance ";
// 内容
$sql = <<<doc
select id,title as shop_name,create_at,master_title,lng,lat,reg_address,province,city,area,address,start_at,end_at,phone,business_name,business_phone,pics,club_id,{$distance}
from jiazhen_shop_info as jsi
where = order by distance ASC,id desc
doc;

php mysql 根据经纬度计算距离和排序的更多相关文章

  1. PHP,Mysql根据经纬度计算距离并排序

    网上给出的计算公式是这样的: Lng1表示A点纬度和经度,Lat2 Lng2 表示B点纬度和经度 a = Lat1 – Lat2为两点纬度之差 b = Lng1 -Lng2 为两点经度之差 6378. ...

  2. MySQL创建根据经纬度计算距离的函数

    按照经纬度计算距离 日常开发中,特别是做微信项目时,经常会遇到根据用户地理位置来展示附近商家的功能,通常解决这种问题的思路是,后台设置商家的经纬度,然后再根据前台传的经纬度进行计算,具体经纬度转换以及 ...

  3. iOS根据2个经纬度计算距离

    #pragma mark - calculate distance 根据2个经纬度计算距离 #define PI 3.14159265358979323 +(double) CalculationDi ...

  4. java工具类(六)根据经纬度计算距离

    Java实现根据经纬度计算距离 在项目开发过程中,需要根据两地经纬度坐标计算两地间距离,所用的工具类如下: Demo1: public static double getDistatce(double ...

  5. python的N个小功能(文本字段对应数值,经纬度计算距离,两个时间点计算时间间隔)

    案例1 >>> import pandas as pd >>> df=pd.DataFrame({'A':[1,2,3],'B':[1,2,3],'C':[1,2, ...

  6. php有经纬度计算距离

    /** *  @desc 根据两点间的经纬度计算距离 *  @param float $lat 纬度值 *  @param float $lng 经度值 */  function getDistanc ...

  7. PHP根据两点间的经纬度计算距离,php两点经纬度计算(转)

    这是一个不错的示例,直接贴代码,首先要知道纬度值.经度值 /** * @desc 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经 ...

  8. PHP根据两点间的经纬度计算距离

    /** * 说明: 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经度值 */ function getDistance($lat ...

  9. cesium根据经纬度计算距离

    var startLatitude = 36;var startLongitude = 120; var endLatitude=34; var endLongitude=121; var start ...

随机推荐

  1. Linux运维技术之详解任务计划(crontab命令)

    crontab命令 按照预先设置的时间周期(分钟.小时.天……)重复执行用户指定的命令操作,属于周期性计划任务 (1).先来看一下/etc/crontab文件的内容: [root@localhost ...

  2. Qt编译报错:The kit Desktop Qt...has configuration issues which might be the root cause for this problem.

    报错:Cannot find file: E:\实验室\20180409_Qt跑马灯\QtTest\QtTest.pro. 17:03:11: 进程"D:\Qt\Qt5.8.0\5.8\ms ...

  3. Centos 7.3 镜像制作

    1.在KVM环境上准备虚拟机磁盘 [root@localhost ~]# qemu-img create -f qcow2 -o size=50G /opt/CentOS---x86_64_50G.q ...

  4. The Preliminary Contest for ICPC Asia Nanjing 2019 H. Holy Grail

    题目链接:https://nanti.jisuanke.com/t/41305 题目说的很明白...只需要反向跑spfa然后输入-dis,然后添-dis的一条边就好了... #include < ...

  5. spring cloud 学习资料

    spring cloud 学习资料 网址 拜托!面试请不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA

  6. Java实现List自定义排序

    废话不多说,直接上代码 import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; i ...

  7. React Hook Flow Diagram

    一.概述 Donovon has created this nice flowchart that explains the new lifecycle of a Hooks component. C ...

  8. UI与数据的绑定

    核心是数据变化跟踪与UI更新的问题 概念整理: 供业务使用的叫数据: 供UI使用的叫状态: UI的变化能被监听到: 数据的变化能实时反映到UI上: 数据变化—>拦截—〉UI状态重置—>UI ...

  9. 什么叫pure function(纯函数)

    (来自:http://en.wikipedia.org/wiki/Pure_function) 在计算机编程中,假如满足下面这两个句子的约束,一个函数可能被描述为一个纯函数: 给出同样的参数值,该函数 ...

  10. golang 几个好用的cli package

    cli 开发在golang 的软件开发中占用很大,同时开源的比较好用的cli也很多,以下是整理的几个cli github.com/spf13/cobra 这个比较有名了, 好多框架都使用了这个 以下是 ...