zoukankan      html  css  js  c++  java
  • PHP,Mysql根据经纬度计算距离并排序

    计算公式:

    mysql:
    //Lng1表示A点纬度和经度,Lat2 Lng2 表示B点纬度和经度
    //a = Lat1 – Lat2为两点纬度之差 b = Lng1 -Lng2 为两点经度之差
    //6378.137为地球半径,单位为公里
    //计算出来的结果单位为公里
     
    
    select *,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(111.86141967773438-lng)/360),2)+COS(PI()*33.07078170776367/180)* COS(lat * PI()/180)*POW(SIN(PI()*(33.07078170776367-lat)/360),2)))) as juli from `area` 
    order by juli asc limit 0,20

    用一句 Mysql 语句,根据经纬度,计算5公里范围内的数据

    select ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-latitude*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(latitude*PI()/180)*POW(SIN(($longitude*PI()/180-longitude*PI()/180)/2),2)))*1000) AS distance FROM shop having distance <= 5000 order by distance asc

     php:

    function GetDistance($lng1,$lat1,$lng2,$lat2){
            //将角度转为狐度
            $radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度
            $radLat2=deg2rad($lat2);
            $radLng1=deg2rad($lng1);
            $radLng2=deg2rad($lng2);
            $a=$radLat1-$radLat2;
            $b=$radLng1-$radLng2;
            $s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137*1000;//计算出来的结果单位为米
    return floor($s); }
    成为不了聪明的人,那就做一个有耐心、肯钻研,坚持不懈,永不放弃的人……
  • 相关阅读:
    组合模式
    迭代器模式
    命令模式
    装饰者模式
    观察者模式
    策略模式
    适配器模式和外观模式
    Servlet
    Java 递归
    Java 反射
  • 原文地址:https://www.cnblogs.com/wrld/p/9377680.html
Copyright © 2011-2022 走看看