FlexibleGeohash是一个处理Geohash的非常快速的库。
Geohash通常用base32编码,但也可以用base2/4/8/16/32编码。
我参考了mmcloughlin/geohash和michael-groble/Geohash来创建此库。
非常感谢!
基准测试
https://gist.github.com/malt03/7ac251ea0f47f4874c986a6720be3acf
解码1,000,000次
库 | 时间 |
---|---|
malt03/FlexibleGeohash | 0.9568190574645996 |
maximveksler/GeohashKit | 1.2289820909500122 |
michael-groble/Geohash | 1.4453539848327637 |
nh7a/Geohash | 9.60263705253601 |
编码1,000,000次
库 | 时间 |
---|---|
malt03/FlexibleGeohash | 0.22908806800842285 |
maximveksler/GeohashKit | 0.9335179328918457 |
michael-groble/Geohash | 0.3671489953994751 |
nh7a/Geohash | 114.44236898422241 |
获取相邻点100,000次
库 | 时间 |
---|---|
malt03/FlexibleGeohash | 0.33184492588043213 |
maximveksler/GeohashKit | 3.7601670026779175 |
michael-groble/Geohash | 0.33417296409606934 |
nh7a/Geohash | 103.8870279788971 |
使用方法
编码
let appleCoordinate = CLLocationCoordinate2D(latitude: 37.331667, longitude: -122.030833)
var apple = Geohash(coordinate: appleCoordinate, precision: 7)
_ = apple.hash() // 9q9hrh5
apple.precision = 4
_ = apple.hash() // 9q9h
apple.precision = 10
_ = apple.hash() // 9q9hrh5ber
解码
var apple = Geohash(hash: "9q9hrh5")
print(apple.region().mk())
// MKCoordinateRegion(
// center: CLLocationCoordinate2D(latitude: 37.33222961425781, longitude: -122.03132629394531),
// span: MKCoordinateSpan(latitudeDelta: 0.001373291015625, longitudeDelta: 0.001373291015625)
// )
apple.precision = 1
print(apple.region().mk())
// MKCoordinateRegion(
// center: CLLocationCoordinate2D(latitude: 59.83154296875, longitude: -99.53201293945312),
// span: __C.MKCoordinateSpan(latitudeDelta: 45.0, longitudeDelta: 45.0)
// )
获取相邻点
var apple = Geohash(hash: "9q9h")
_ = apple.neighbor(.north).hash() // 9q9j
_ = apple.neighbors().map { $0.hash() } // ["9q9k", "9q9m", "9q97", "9q9j", "9q95", "9q8u", "9q8v", "9q8g"]
apple.precision = 1
_ = apple.neighbors().map { $0.hash() } // ["d", "f", "6", "c", "3", "8", "b", "2"]
apple.precision = 7
_ = apple.neighbors().map { $0.hash() } // ["9q9h001", "9q9h003", "9q95bpc", "9q9h002", "9q95bpb", "9q8upbp", "9q8upbr", "9q8gzzz"]
使用 base8 代码
当然,除 base32 以外的任何编码方式生成的 Geohash 编码与 base32 编码的是不兼容的。
let appleCoordinate = CLLocationCoordinate2D(latitude: 37.331667, longitude: -122.030833)
var apple = Geohash(coordinate: appleCoordinate, precision: 21, encoding: .base8)
_ = apple.hash() // 233114136012515572573
apple.precision = 4
_ = apple.hash() // 2331
apple.encoding = .base32
apple.precision = 12
_ = apple.hash() // 9q9hrh5berpg
_ = Geohash(hash: "2331", encoding: .base8).region().mk()
// MKCoordinateRegion(
// center: CLLocationCoordinate2D(latitude: 37.96875, longitude: -120.9375),
// span: MKCoordinateSpan(latitudeDelta: 2.8125, longitudeDelta: 5.625)
// )
获取精度范围的代码
_ = Geohash.span(precision: 7) // latitude: 0.001373291015625, longitude: 0.001373291015625
_ = Geohash.span(precision: 7, encoding: .base2) // latitude: 22.5, longitude: 22.5
设置默认精度/编码
Geohash.defaultPrecision = 4
Geohash.defaultEncoding = .base2
_ = Geohash(coordinate: LatLng(37.331667, -122.030833)).hash() // "0100"
_ = Geohash(hash: "0100").region().mk()
// MKCoordinateRegion(
// center: CLLocationCoordinate2D(latitude: 22.5, longitude: -135),
// span: MKCoordinateSpan(latitudeDelta: 45, longitudeDelta: 90)
// )
安装
SwiftPM (推荐)
- 在 Xcode 中,点击
文件
>Swift 包
>添加包依赖...
- 输入
https://github.com/malt03/FlexibleGeohash.git
CocoaPods
- 在你的 Podfile 中插入
pod 'FlexibleGeohash'
- 运行
pod install