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

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))
    }
}

本文来自:开源中国博客

感谢作者:bluezwt

查看原文:geohash算法以及求最邻近区域的点-golang

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。