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

bluezwt · 2014-12-30 13:00:01 · 10131 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览    
这是一个创建于 2014-12-30 13:00:01 的文章,其中的信息可能已经有所发展或是发生改变。

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

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

10131 次点击  
加入收藏 微博
上一篇:Golang计算MD5
下一篇:golang daemon
4 回复  |  直到 2000-01-01 00:00:00
HeinoC
HeinoC · #1 · 10年之前

呃。。。。格式化一下代码会让人更好读一些。。

polaris
polaris · #2 · 10年之前
HeinoCHeinoC #1 回复

呃。。。。格式化一下代码会让人更好读一些。。

已经格式化

15584321
15584321 · #3 · 10年之前

你好。请问如何根据当前点hash,找出它附件的8个区域呢?

15584321
15584321 · #4 · 10年之前

GetNeighbors,是找当前区域的?还是覆盖了周边几个区域内的??

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传