Geohash:如何计算周围的八个方框

时间:2014-02-09 12:20:58

标签: javascript php geohashing

我使用https://github.com/davetroy/geohash-js中的代码 但我不知道如何计算给定geohashcode的周围geohashcode。 我需要在php和javascript中使用函数计算它们。 有没有人有这样的代码或某种方法来解决这个问题?

2 个答案:

答案 0 :(得分:1)

https://github.com/davetroy/geohash-js阅读自述文件... 在javascript中,您可以通过调用例如

来创建GeoHash
mygh= encodeGeoHash( 53.1, -0.24 );

这将给你一个像'gcry4d91zt7n'的值,如果你再对此进行解码:

mydecoded= decodeGeoHash(mygh);

你得到一个包含以下内容的对象:

Object {latitude: Array[3], longitude: Array[3]}
latitude: Array[3]
0: 53.099999986588955
1: 53.10000015422702
2: 53.10000007040799

longitude: Array[3]
0: -0.24000003933906555
1: -0.2399997040629387
2: -0.23999987170100212 

如果您只是将geohash截断为'gcry',那么您已经降低了分辨率,并且对decodeGeoHash的调用将返回:

Object {latitude: Array[3], longitude: Array[3]}
latitude: Array[3]
0: 53.0859375
1: 53.26171875
2: 53.173828125

longitude: Array[3]
0: -0.3515625
1: 0
2: -0.17578125

即。这三点现在描述了一个更大的区域。正如自述文件中所提到的,这个较大的框不是以原始(较高分辨率)框为中心 - 因为您需要使用例如计算相邻(顶部,底部,左侧,右侧)地理相位。

mygh_top= calculateAdjacent( mygh, 'top')

查看geohash-demo.js的来源,其中显示了一个示例。

答案 1 :(得分:1)

https://github.com/davetroy/geohash-js/blob/master/geohash-demo.js#L63有例外:

GeoHashBox.prototype.showNeighbors = function () {
var geohashPrefix = this.geohash.substr(0,this.geohash.length-1);

this.neighbors.top = new GeoHashBox(calculateAdjacent(this.geohash, 'top'));
this.neighbors.bottom = new GeoHashBox(calculateAdjacent(this.geohash, 'bottom'));
this.neighbors.right = new GeoHashBox(calculateAdjacent(this.geohash, 'right'));
this.neighbors.left = new GeoHashBox(calculateAdjacent(this.geohash, 'left'));
this.neighbors.topleft = new GeoHashBox(calculateAdjacent(this.neighbors.left.geohash, 'top'));
this.neighbors.topright = new GeoHashBox(calculateAdjacent(this.neighbors.right.geohash, 'top'));
this.neighbors.bottomright = new GeoHashBox(calculateAdjacent(this.neighbors.right.geohash, 'bottom'));
this.neighbors.bottomleft = new GeoHashBox(calculateAdjacent(this.neighbors.left.geohash, 'bottom'));
}