• Sphinx的GEO距离搜索 [转载]


    近项目用sphinx做地理搜索,可是结果总是不对,明明很近却搜不到

    结果检查sphinx源文件:

    static inline double sphSqr ( double v )
    
    {
    
    return v*v;
    
    }
    
    float ExprGeodist_t::Eval ( const CSphMatch & tMatch ) const
    
    {
    
    const double R = 6384000;
    
    float plat = tMatch.GetAttrFloat ( m_tGeoLatLoc );
    
    float plon = tMatch.GetAttrFloat ( m_tGeoLongLoc );
    
    double dlat = plat - m_fGeoAnchorLat;
    
    double dlon = plon - m_fGeoAnchorLong;
    
    double a = sphSqr ( sin ( dlat/2 ) ) + cos(plat)*cos(m_fGeoAnchorLat)*sphSqr(sin(dlon/2));
    
    double c = 2*asin ( Min ( 1, sqrt(a) ) );
    
    return (float)(R*c);
    
    }
    

      

    附上解决办法:

    $search->SetGeoAnchor('latitude', 'longitude', (float) deg2rad($_latitude), (float) deg2rad($_longitude));

    传入坐标的时候把坐标角度转换成弧度即可

    PHP版弧度计算:

    function sphSqr($v){

    return $v*$v;

    }

    function cacu_geo($loc_lat,$loc_lng,$geo_lat,$geo_lng){

    $R = 6384000;

    $plat = (float)$loc_lat;

    $plon = (float)$loc_lng;

    $dlat = $plat - $geo_lat;

    $dlon = $plon - $geo_lng;

    $a = sphSqr ( sin ( $dlat/2 ) ) + cos($plat)*cos($geo_lat)*sphSqr(sin($dlon/2));

    $c = 2*asin ( min ( 1, sqrt($a) ) );

    return (float)($R*$c);

    }

    MYSQL版本坐标计算(角度):

    SELECT 6384000 * 2 * ASIN(SQRT(

    POWER(SIN((31.218992 - abs(31.218992)) * pi()/180 / 2), 

    2) +  COS(31.218992 * pi()/180 ) * COS(abs(31.218992) * 

    pi()/180) *  POWER(SIN((121.431241 - 121.431480) * 

    pi()/180 / 2), 2) )) as  distance

    PHP版本坐标计算(角度):

    function rad($d)

    {

        return $d * 3.1415926535898 / 180.0;

    }

    function GetDistance($lat1, $lng1, $lat2, $lng2)

    {

        $EARTH_RADIUS = 6384.000;

        $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;

    }

  • 相关阅读:
    HUD——T 3836 Equivalent Sets
    HDU——T 2594 Simpsons’ Hidden Talents
    vertical-align和line-height的深入应用
    November 7th 2016 Week 46th Monday
    November 6th 2016 Week 46th Sunday
    November 5th Week 45th Saturday 2016
    November 4th Week 45th Friday 2016
    【2017-01-08】QTimer与QThread的调度时间精度
    November 3rd Week 45th Thursday 2016
    November 2nd Week 45th Wednesday 2016
  • 原文地址:https://www.cnblogs.com/jamesbd/p/4698777.html
Copyright © 2020-2023  润新知