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)
near.AddCoord("A", 40.92424, 116.3906)
near.AddCoord("B", 39.93224, 116.3927)
near.AddCoord("C", 39.92484, 116.3916)
near.AddCoord("D", 39.92494, 116.3923)
near.AddCoord("E", 39.92220, 116.3915)
near.AddCoord("F", 39.92424, 117.3906)
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))
}
}