首页 > 代码库 > PHP之路——geohash查找附近的人

PHP之路——geohash查找附近的人

<?php
	
class location
{
	public function getLat($number, $small, $big,  $temp = 20){
		$temp--;
		$average = ($big + $small) / 2;
		if ($temp == 0) {
			if ($average <= $number) {
				return ‘1‘;
			} else {
				return ‘0‘;
			}
		}
		if ($average <= $number) {
			return ‘1‘ . $this->getLat($number, $average, $big, $temp);
		} else {
			return ‘0‘ . $this->getLat($number, $small, $average, $temp);
		}
	}

	public function joinStr($lat, $lon){
		$len = strlen($lat);
		$str = ‘‘;
		for ($i=0; $i < $len; $i++) { 
			$str .= $lon[$i] . $lat[$i];
		}
		return $str;
	}
}

$location = new location();
$lat = $location->getLat(39.92324, -90, 90);
$lon = $location->getLat(116.3906, -180, 180);
$str =  $location->joinStr($lat, $lon);
for ($i=0; $i <strlen($str) / 5 ; $i++) { 
	$code[$i] = bindec(substr($str, $i * 5, 5));
}
foreach ($code as $key => $value) {
	echo $value . "\n";
}

  

PHP之路——geohash查找附近的人