Skip to content

Commit ab97b6a

Browse files
author
hero
committed
redis geohash
1 parent 5b8c93d commit ab97b6a

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

geo/redis/main.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/go-redis/redis"
6+
)
7+
8+
func main() {
9+
GlobalClient := redis.NewClient(
10+
&redis.Options{
11+
Addr: "127.0.0.1:6379",
12+
},
13+
)
14+
err := GlobalClient.Ping().Err()
15+
if nil != err {
16+
panic(err)
17+
}
18+
fmt.Println("链接redis成功")
19+
res, err := GlobalClient.GeoAdd("geo_hash_test", &redis.GeoLocation{
20+
Name: "天府广场",
21+
Longitude: 104.072833,
22+
Latitude: 30.663422,
23+
}, &redis.GeoLocation{
24+
Name: "四川大剧院",
25+
Longitude: 104.074378,
26+
Latitude: 30.664804,
27+
}, &redis.GeoLocation{
28+
Name: "新华文轩",
29+
Longitude: 104.070084,
30+
Latitude: 30.664649,
31+
}, &redis.GeoLocation{
32+
Name: "手工茶",
33+
Longitude: 104.072402,
34+
Latitude: 30.664121,
35+
}, &redis.GeoLocation{
36+
Name: "宽窄巷子",
37+
Longitude: 104.059826,
38+
Latitude: 30.669883,
39+
}, &redis.GeoLocation{
40+
Name: "奶茶",
41+
Longitude: 104.06085,
42+
Latitude: 30.670054,
43+
}, &redis.GeoLocation{
44+
Name: "钓鱼台",
45+
Longitude: 104.058424,
46+
Latitude: 30.670737,
47+
}).Result()
48+
if err != nil {
49+
panic(err)
50+
}
51+
fmt.Println("[GeoAdd]", res)
52+
resPos, err := GlobalClient.GeoPos("geo_hash_test", "天府广场", "宽窄巷子").Result()
53+
if err != nil {
54+
panic(err)
55+
}
56+
for _, v := range resPos {
57+
fmt.Println("[GeoPos]", "Longitude : ", v.Longitude, "Latitude : ", v.Latitude)
58+
}
59+
//其中 unit 参数是距离单位
60+
//m 表示单位为米。
61+
//km 表示单位为千米。
62+
//mi 表示单位为英里。
63+
//ft 表示单位为英尺。
64+
resDist, err := GlobalClient.GeoDist("geo_hash_test", "天府广场", "宽窄巷子", "m").Result()
65+
if err != nil {
66+
panic(err)
67+
}
68+
fmt.Println("[GeoDist]", resDist)
69+
70+
resRadiu, err := GlobalClient.GeoRadius("geo_hash_test", 104.072833, 30.663422, &redis.GeoRadiusQuery{
71+
Radius: 800, //radius表示范围距离,
72+
Unit: "m", //距离单位是 m|km|ft|mi
73+
WithCoord: true, //传入WITHCOORD参数,则返回结果会带上匹配位置的经纬度
74+
WithDist: true, //传入WITHDIST参数,则返回结果会带上匹配位置与给定地理位置的距离。
75+
WithGeoHash: true, //传入WITHHASH参数,则返回结果会带上匹配位置的hash值。
76+
Count: 4, //入COUNT参数,可以返回指定数量的结果。
77+
Sort: "ASC", //默认结果是未排序的,传入ASC为从近到远排序,传入DESC为从远到近排序。
78+
}).Result()
79+
if err != nil {
80+
panic(err)
81+
}
82+
for _,v:=range resRadiu{
83+
fmt.Println("[GeoRadiu]", v)
84+
}
85+
86+
resRadiusByMember, err := GlobalClient.GeoRadiusByMember("geo_hash_test", "天府广场", &redis.GeoRadiusQuery{
87+
Radius: 800,
88+
Unit: "m",
89+
WithCoord: true,
90+
WithDist: true,
91+
WithGeoHash: true,
92+
Count: 4,
93+
Sort: "ASC",
94+
}).Result()
95+
if err != nil {
96+
panic(err)
97+
}
98+
99+
for _,v:=range resRadiusByMember{
100+
fmt.Println("[GeoRadiusByMember]", v)
101+
}
102+
resHash, err := GlobalClient.GeoHash("geo_hash_test", "天府广场").Result()
103+
if err != nil {
104+
panic(err)
105+
}
106+
fmt.Println("[GeoHash]", resHash)
107+
}

0 commit comments

Comments
 (0)