GithubHelp home page GithubHelp logo

gongdexing / geohash Goto Github PK

View Code? Open in Web Editor NEW
264.0 11.0 81.0 12 KB

GeoHash是目前比较主流实现位置服务的技术,用最简洁的Java实现GeoHash算法

Java 100.00%
java mysql geohash lbs latitude longitude

geohash's Issues

如何控制精度?

需求:得到一个位置范围10公里、范围1公里、范围100米... 的8个周围geohash?

请教个问题

如果我有一个经纬度,比如116.425884,39.921842,使用geohash得到wx4g17d9xd,这个字符串表示一个矩形区域,那为什么不直接省略经纬度小数,比如116.42,39.92这样表示区域呢?直接省略小数有什么问题吗

geohash 编码错误

文章的经纬度搞反了,

1110011101001000111100000011100111001101 base32 编码后是,不是 wx4g0ffe 是45EPAOON

你这个解释叼。网上一堆解释,还各种图片,花里胡哨看不懂

你这个前面的 栗子 + 原理 两段,我就给整明白了,,,谢谢~

很快我就用 PHP 代码实现一下,当然 base32 那里是照抄你的,

算出的结果一样,

public function testGeoHash()
{
    dump($this->geoHash(116.402843, 39.999375)); // 鸟巢
    dump($this->geoHash(116.3967, 39.99932)); // 水立方
    dump($this->geoHash(116.40382, 39.918118)); // 故宫
}

public function geoHash(float $lon, float $lat): string
{
    $lonBin = $this->geoToBin($lon, -180.0, 180.0);
    $latBin = $this->geoToBin($lat, -90.0, 90.0);

    $bin = $this->geoMerge($lonBin, $latBin);

    $hash = $this->base32($bin);

    return $hash;
}

protected function geoToBin(float $val, float $min, float $max): string
{
    $res = '';
    for ($i = 0; $i < 20; $i++) {
        $middle = ($max + $min) / 2;
        if ($val <= $middle) {
            $res .= '0';
            $max = $middle;
        } else {
            $res .= '1';
            $min = $middle;
        }
    }

    return $res;
}

protected function geoMerge(string $lonBin, string $latBin): string
{
    $res = '';
    for ($i = 0; $i < 20; $i++) {
        $res .= $lonBin[$i].$latBin[$i];
    }

    return $res;
}

protected function base32(string $str): string
{
    $base32Alphabet = '0123456789bcdefghjkmnpqrstuvwxyz';

    $len = strlen($str);
    $step = (int) ($len / 8); // 转成 8 位的 base32 编码

    $res = '';

    for ($i = 0; $i < $len; $i += $step) {
        $subStr = substr($str, $i, $step);
        $subLen = strlen($subStr);

        $index = 0;
        for ($j = 0; $j < $subLen; $j++) {
            $index += ($subStr[$j] === '0')
                ? 0
                : (1 << ($subLen - 1 - $j));
        }

        $res .= $base32Alphabet[$index];
    }

    return $res;
}

GeoHash编码可能有误

文章中说“[116.3967,44.9999]和[116.3967,45.0009]的Geohash分别是wxfzbxvr和y84b08j2”,我没有跑你的代码,我在在线网站上试着生成GeoHash码,发现和你的编码不一样的,在线生成的hash码是“y84b08jm”,不知道是不是你笔误,还是编码有问题。在线链接http://www.movable-type.co.uk/scripts/geohash.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.