gongdexing / geohash Goto Github PK
View Code? Open in Web Editor NEWGeoHash是目前比较主流实现位置服务的技术,用最简洁的Java实现GeoHash算法
GeoHash是目前比较主流实现位置服务的技术,用最简洁的Java实现GeoHash算法
需求:得到一个位置范围10公里、范围1公里、范围100米... 的8个周围geohash?
如果我有一个经纬度,比如116.425884,39.921842,使用geohash得到wx4g17d9xd,这个字符串表示一个矩形区域,那为什么不直接省略经纬度小数,比如116.42,39.92这样表示区域呢?直接省略小数有什么问题吗
文章的经纬度搞反了,
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;
}
文章中说“[116.3967,44.9999]和[116.3967,45.0009]的Geohash分别是wxfzbxvr和y84b08j2”,我没有跑你的代码,我在在线网站上试着生成GeoHash码,发现和你的编码不一样的,在线生成的hash码是“y84b08jm”,不知道是不是你笔误,还是编码有问题。在线链接http://www.movable-type.co.uk/scripts/geohash.html
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.