仅仅写了一个demo
func Handle() {
//添加地址
local1 := redis.GeoLocation{ //上海
Name: "J4828A2109240076",
Longitude: 121.477106,
Latitude: 31.261318,
}
// local2 := redis.GeoLocation{ //四川
// Name: "J4828A2109240503",
// Longitude: 104.386720,
// Latitude: 31.111256,
// }
local2 := redis.GeoLocation{ //广西
Name: "J4828A2109240503",
Longitude: 111.012977,
Latitude: 25.848181,
}
local3 := redis.GeoLocation{ //广东
Name: "J4828A2109240460",
Longitude: 113.535330,
Latitude: 22.530372,
}
err := Redis.GeoAdd(Ctx, "cabinet", &local1, &local2, &local3).Err()
if err != nil {
fmt.Println("reis geo add 获取数据失败:", err)
return
}
//计算两地距离
distance, err := Redis.GeoDist(Ctx, "cabinet", "J4828A2109240076", "J4828A2109240503", "km").Result()
if err != nil {
panic(err)
}
fmt.Println("GeoDist: ", distance, "km")
//当前位置,上海利物盛
long := 121.348042
lat := 31.306801
radius, _ := Redis.GeoRadius(Ctx, "cabinet", long, lat, &redis.GeoRadiusQuery{
Radius: 80000,
Unit: "km",
WithCoord: true, // WITHCOORD参数,返回结果会带上匹配位置的经纬度
WithDist: true, // WITHDIST参数,返回结果会带上匹配位置与给定地理位置的距离。
// WithGeoHash: true, // WITHHASH参数,返回结果会带上匹配位置的hash值。
Count: 4, // COUNT参数,可以返回指定数量的结果。
Sort: "DESC", // 传入ASC为从近到远排序,传入DESC为从远到近排序。
}).Result()
for _, v := range radius {
fmt.Println("GeoRadius: ", v)
}
}

网友评论