（1）GO源码：github.com/golang/geo

（3）唯一的pdf：pan.baidu.com/s/1gfxJB0J

Why not Geohash?

（1）Geohash is a great solution to perform geo coordinates queries but the way it works can sometimes be an issue with your data.

（2）Remember geohashes are cells of 12 different widths from 5000km to 3.7cm, when you perform a lookup around a position, if your position is close to a cell’s edge you could miss some points from the adjacent cell, that’s why you have to query for the 8 neightbour cells, it means 9 range queries into your DB to find all the closest points from your location.

（3）If your lookup does not fit in level439km by 19.5km, the next level is 156km by 156km!

（4）The query is not performed around your coordinates, you search for the cell you are in then you query for the adjacent cells at the same level/radius, based on your needs, it means it works very approximately and you can only perform ‘circles’ lookup around the cell you are in.

（5）The most precise geohash needs 12 bytes storage.

（6）-90 +90 and +180 -180, -0 +0 are not sides by sides prefixes.

Why S2?

（1）S2 cells have a level ranging from30~0.7cm² to0~85,000,000km².

（2）S2 cells are encoded on an uint64, easy to store.

（3）The main advantage is the region coverer algorithm, give it a region and the maximum number of cells you want, S2 will return some cells atdifferent levels that cover the region you asked for, remember one cell corresponds to a range lookup you’ll have to perform in your database.

（4）The coverage is more accurate it means less read from the DB, less objects unmarshalling…

NOW ： 让我们闭上眼睛，想象你的前方有一个地球，地球外表有个正立方体，刚好与地球外切。

p呢，就是地球上的一个点，p的坐标呢我们可以表示成三围坐标系的空间坐标（x,y,z）不信，你就再看看上边吧，谁让你数学不好呢。

（1）线性变换，就是不做任何处理，就是跟上图差不多

（2）针对（x,y）每个值u，做

（3）针对（x,y）每个值u，做

p是（s, t） 其中 -1 < s , t< 1

Math.max(0,Math.min(2*m-1,Math.round(m*s+(m-0.5))))