geohash算法以及求最邻近区域的点-golang

bluezwt · · 9523 次点击 · · 开始浏览

geohash算法以及求最邻近区域的点，对这两个算法封装成了golang package，写LBS服务程序的时候有用。

https://github.com/gansidui/geohash

https://github.com/gansidui/nearest

gohash

``````package main

import (
"fmt"
"github.com/gansidui/geohash"
)

func main() {
latitude := 39.92324
longitude := 116.3906
precision := 5
hash, box := geohash.Encode(latitude, longitude, precision)

fmt.Println(hash)
fmt.Println(box.MinLat, box.MaxLat, box.MinLng, box.MaxLng)

neighbors := geohash.GetNeighbors(latitude, longitude, precision)
for _, hash = range neighbors {
fmt.Print(hash, " ")
}
}
``````

nearest

``````package main

import (
"fmt"
"github.com/gansidui/nearest"
)

func main() {
near := nearest.NewNearest()
near.SetPrecision(5)
keys := near.QueryNearestSquareFromKey("C")
coordNode1, ok := near.GetCoordNode("C")
if !ok {
return
}
for _, key := range keys {
coordNode2, _ := near.GetCoordNode(key)
fmt.Println(key, nearest.DistanceCoordNode(coordNode1, coordNode2))
}
}
``````

4 回复  |  直到 2000-01-01 00:00:00

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

geohash算法以及求最邻近区域的点，对这两个算法封装成了golang package，写LBS服务程序的时候有用。

https://github.com/gansidui/geohash

https://github.com/gansidui/nearest

gohash

``````package main

import (
"fmt"
"github.com/gansidui/geohash"
)

func main() {
latitude := 39.92324
longitude := 116.3906
precision := 5
hash, box := geohash.Encode(latitude, longitude, precision)

fmt.Println(hash)
fmt.Println(box.MinLat, box.MaxLat, box.MinLng, box.MaxLng)

neighbors := geohash.GetNeighbors(latitude, longitude, precision)
for _, hash = range neighbors {
fmt.Print(hash, " ")
}
}
``````

nearest

``````package main

import (
"fmt"
"github.com/gansidui/nearest"
)

func main() {
near := nearest.NewNearest()
near.SetPrecision(5)