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;

}

原文地址:https://www.cnblogs.com/jamesbd/p/4698777.html