Geohash::encode PHP Метод

encode() публичный Метод

Encode a hash from given lat and long
public encode ( $lat, $long )
    public function encode($lat, $long)
    {
        //how many bits does latitude need?
        $plat = $this->precision($lat);
        $latbits = 1;
        $err = 45;
        while ($err > $plat) {
            $latbits++;
            $err /= 2;
        }
        //how many bits does longitude need?
        $plong = $this->precision($long);
        $longbits = 1;
        $err = 90;
        while ($err > $plong) {
            $longbits++;
            $err /= 2;
        }
        //bit counts need to be equal
        $bits = max($latbits, $longbits);
        //as the hash create bits in groups of 5, lets not
        //waste any bits - lets bulk it up to a multiple of 5
        //and favour the longitude for any odd bits
        $longbits = $bits;
        $latbits = $bits;
        $addlong = 1;
        while (($longbits + $latbits) % 5 != 0) {
            $longbits += $addlong;
            $latbits += !$addlong;
            $addlong = !$addlong;
        }
        //encode each as binary string
        $blat = $this->binEncode($lat, -90, 90, $latbits);
        $blong = $this->binEncode($long, -180, 180, $longbits);
        //merge lat and long together
        $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;
        }
        //convert binary string to hash
        $hash = "";
        for ($i = 0; $i < strlen($binary); $i += 5) {
            $n = bindec(substr($binary, $i, 5));
            $hash = $hash . $this->coding[$n];
        }
        return $hash;
    }

Usage Example

Пример #1
0
<?php

error_reporting(E_ALL);
echo "version:" . GEOHASH_VERSION . "\n";
echo microtime(true) . "\n";
$geohash = "wm3yr31d2524";
$coord = Geohash::decode($geohash);
print_r($coord);
var_dump($geohash, $coord);
$geohash = Geohash::encode($coord['latitude'], $coord['longitude']);
print_r(Geohash::encode(30.635780068114, 104.03160111979, 12));
var_dump($geohash);
echo microtime(true) . "\n";
All Usage Examples Of Geohash::encode