您的位置:首页 > 其它

关于计算周围多少里以内的楼盘

2013-04-23 15:22 197 查看
/**
*根据经纬度计算距离其中A($lat1,$lng1)、B($lat2,$lng2)
*注意弧度角度的计算
*单位:km
*/
function_getDistance($lat1,$lng1,$lat2,$lng2)
{
//地球半径
$R=6378.137;//km

//将角度转为狐度
$radLat1=deg2rad($lat1);
$radLat2=deg2rad($lat2);
$radLng1=deg2rad($lng1);
$radLng2=deg2rad($lng2);

//结果
$s=acos(cos($radLat1)*cos($radLat2)*cos($radLng1-$radLng2)+sin($radLat1)*sin($radLat2))*$R;

//精度
$s=round($s*10000)/10000;
returnround($s);
}




/**
*根据传入的中心点的经纬度和半径,计算出矩形区域
*@paramfloat$center_lat
*@paramfloat$center_lng
*@paramint$radiusunit:km
*/
functiongetAroundRectangle($center_lat,$center_lng,$radius)
{
//先来求东西两侧的的范围边界经度
$earth_radius=6378.137;//km
$dlng=rad2deg(2*asin(sin($radius/(2*$earth_radius))/cos(deg2rad($center_lat))));//角度

//然后求南北两侧的范围边界维度
$dlat=rad2deg($radius/$earth_radius);

$data=array(
'lat_min'=>$center_lat-$dlat,//维度最小
'lat_max'=>$center_lat+$dlat,//唯独最大
'lng_min'=>$center_lng-$dlng,//经度最小
'lng_max'=>$center_lng+$dlng,//经度最大
);
return$data;
}






ViewCode

/**
*Geohashgenerationclass
*http://blog.dixo.net/downloads/*
*Thisfilecopyright(C)2008PaulDixon(paul@elphin.com)
*
*Thisprogramisfreesoftware;youcanredistributeitand/or
*modifyitunderthetermsoftheGNUGeneralPublicLicense
*aspublishedbytheFreeSoftwareFoundation;eitherversion3
*oftheLicense,or(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNUGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/

/**
*Encodeanddecodegeohashes
*
*/
classGeohash
{
private$coding="0123456789bcdefghjkmnpqrstuvwxyz";
private$codingMap=array();

publicfunctionGeohash()
{
//buildmapfromencodingcharto0paddedbitfield
for($i=0;$i<32;$i++)
{
$this->codingMap[substr($this->coding,$i,1)]=str_pad(decbin($i),5,"0",STR_PAD_LEFT);
}

}

/**
*Decodeageohashandreturnanarraywithdecimallat,longinit
*/
publicfunctiondecode($hash)
{
//decodehashintobinarystring
$binary="";
$hl=strlen($hash);
for($i=0;$i<$hl;$i++)
{
$binary.=$this->codingMap[substr($hash,$i,1)];
}

//splitthebinaryintolatandlogbinarystrings
$bl=strlen($binary);
$blat="";
$blong="";
for($i=0;$i<$bl;$i++)
{
if($i%2)
$blat=$blat.substr($binary,$i,1);
else
$blong=$blong.substr($binary,$i,1);

}

//nowconcerttodecimal
$lat=$this->binDecode($blat,-90,90);
$long=$this->binDecode($blong,-180,180);

//figureouthowprecisethebitcountmakesthiscalculation
$latErr=$this->calcError(strlen($blat),-90,90);
$longErr=$this->calcError(strlen($blong),-180,180);

//howmanydecimalplacesshouldweuse?There'salittleartto
//thistoensureIgetthesameroundingsasgeohash.org
$latPlaces=max(1,-round(log10($latErr)))-1;
$longPlaces=max(1,-round(log10($longErr)))-1;

//roundit
$lat=round($lat,$latPlaces);
$long=round($long,$longPlaces);

returnarray($lat,$long);
}

/**
*Encodeahashfromgivenlatandlong
*/
publicfunctionencode($lat,$long)
{
//howmanybitsdoeslatitudeneed?
$plat=$this->precision($lat);
$latbits=1;
$err=45;
while($err>$plat)
{
$latbits++;
$err/=2;
}

//howmanybitsdoeslongitudeneed?
$plong=$this->precision($long);
$longbits=1;
$err=90;
while($err>$plong)
{
$longbits++;
$err/=2;
}

//bitcountsneedtobeequal
$bits=max($latbits,$longbits);

//asthehashcreatebitsingroupsof5,letsnot
//wasteanybits-letsbulkituptoamultipleof5
//andfavourthelongitudeforanyoddbits
$longbits=$bits;
$latbits=$bits;
$addlong=1;
while(($longbits+$latbits)%5!=0)
{
$longbits+=$addlong;
$latbits+=!$addlong;
$addlong=!$addlong;
}

//encodeeachasbinarystring
$blat=$this->binEncode($lat,-90,90,$latbits);
$blong=$this->binEncode($long,-180,180,$longbits);

//mergelatandlongtogether
$binary="";
$uselong=1;
while(strlen($blat)+strlen($blong))
{
if($uselong)
{
$binary=$binary.substr($blong,0,1);
$blong=substr($blong,1);
}
else
{
$binary=$binary.substr($blat,0,1);
$blat=substr($blat,1);
}
$uselong=!$uselong;
}

//convertbinarystringtohash
$hash="";
for($i=0;$i<strlen($binary);$i+=5)
{
$n=bindec(substr($binary,$i,5));
$hash=$hash.$this->coding[$n];
}

return$hash;
}

/**
*What'sthemaximumerrorfor$bitsbitscoveringarange$minto$max
*/
privatefunctioncalcError($bits,$min,$max)
{
$err=($max-$min)/2;
while($bits--)
$err/=2;
return$err;
}

/*
*returnsprecisionofnumber
*precisionof42is0.5
*precisionof42.4is0.05
*precisionof42.41is0.005etc
*/
privatefunctionprecision($number)
{
$precision=0;
$pt=strpos($number,'.');
if($pt!==false)
{
$precision=-(strlen($number)-$pt-1);
}

returnpow(10,$precision)/2;
}

/**
*createbinaryencodingofnumberasdetailedinhttp://en.wikipedia.org/wiki/Geohash#Example*removingthetailrecursionisleftanexerciseforthereader
*/
privatefunctionbinEncode($number,$min,$max,$bitcount)
{
if($bitcount==0)
return"";

#echo"$bitcount:$min$max<br>";

//thisisourmidpoint-wewillproduceabittosay
//whether$numberisaboveorbelowthismidpoint
$mid=($min+$max)/2;
if($number>$mid)
return"1".$this->binEncode($number,$mid,$max,$bitcount-1);
else
return"0".$this->binEncode($number,$min,$mid,$bitcount-1);
}

/**
*decodesbinaryencodingofnumberasdetailedinhttp://en.wikipedia.org/wiki/Geohash#Example*removingthetailrecursionisleftanexerciseforthereader
*/
privatefunctionbinDecode($binary,$min,$max)
{
$mid=($min+$max)/2;

if(strlen($binary)==0)
return$mid;

$bit=substr($binary,0,1);
$binary=substr($binary,1);

if($bit==1)
return$this->binDecode($binary,$mid,$max);
else
return$this->binDecode($binary,$min,$mid);
}
}


方案1:

  根据中心点,和上面的算法计算出几公里以内的最大/最小经纬度,然后搜索时用这个条件(我们想要的为圆型的,需要过滤一次数据在),使用于数据量相对较小的

缺点:1.范围比较的索引利用率并不高,2.SQL语句极其不稳定(不同的当前位置会产生完全不同的SQL查询),很难缓存。

方案2:

  运用geohash,geohash是一种地址编码,它能把二维的经纬度编码成一维的字符串,字符串匹配度越大,离的越近,适用于数据量较大的,

缺点:匹配程度并不能准确控制距离,只能找出比他大的范围,然后在用程序去判断

文章链接:

http://tech.idv2.com/2011/06/17/location-search/

http://tech.idv2.com/2011/07/05/geohash-intro/

http://www.wubiao.info/401

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐