golang 通过 Redis GEO 实现 LBS 功能

2020-03-17  本文已影响0人  hwholiday

LBS

redis 中关于 GEO 的方法

geoadd 使用方法

res, err := GlobalClient.GeoAdd("geo_hash_test", &redis.GeoLocation{
        Name:      "天府广场",
        Longitude: 104.072833,
        Latitude:  30.663422,
    }, &redis.GeoLocation{
        Name:      "四川大剧院",
        Longitude: 104.074378,
        Latitude:  30.664804,
    }, &redis.GeoLocation{
        Name:      "新华文轩",
        Longitude: 104.070084,
        Latitude:  30.664649,
    }, &redis.GeoLocation{
        Name:      "手工茶",
        Longitude: 104.072402,
        Latitude:  30.664121,
    }, &redis.GeoLocation{
        Name:      "宽窄巷子",
        Longitude: 104.059826,
        Latitude:  30.669883,
    }, &redis.GeoLocation{
        Name:      "奶茶",
        Longitude: 104.06085,
        Latitude:  30.670054,
    }, &redis.GeoLocation{
        Name:      "钓鱼台",
        Longitude: 104.058424,
        Latitude:  30.670737,
    }).Result()

//输出
//[GeoAdd] 7    

geopos 使用方法

resPos, err := GlobalClient.GeoPos("geo_hash_test", "天府广场", "宽窄巷子").Result()

//输出
//[GeoPos] Longitude :  104.07283455133438 Latitude :  30.663422957895442
//[GeoPos] Longitude :  104.05982583761215 Latitude :  30.66988396213059

geohash 使用方法

resHash, err := GlobalClient.GeoHash("geo_hash_test", "天府广场").Result()

//输出
//[GeoHash] [wm6n2npe2u0]   

geodist 使用方法

resDist, err := GlobalClient.GeoDist("geo_hash_test", "天府广场", "宽窄巷子", "m").Result()

//输出
//[GeoDist] 1437.137

georadius 使用方法

resRadiu, err := GlobalClient.GeoRadius("geo_hash_test", 104.072833, 30.663422, &redis.GeoRadiusQuery{
        Radius:      800,   //radius表示范围距离,
        Unit:        "m",   //距离单位是 m|km|ft|mi
        WithCoord:   true, //传入WITHCOORD参数,则返回结果会带上匹配位置的经纬度
        WithDist:    true, //传入WITHDIST参数,则返回结果会带上匹配位置与给定地理位置的距离。
        WithGeoHash: true, //传入WITHHASH参数,则返回结果会带上匹配位置的hash值。
        Count:       4,     //入COUNT参数,可以返回指定数量的结果。
        Sort:        "ASC", //默认结果是未排序的,传入ASC为从近到远排序,传入DESC为从远到近排序。
    }).Result()

//输出
//[GeoRadiu] {天府广场 104.07283455133438 30.663422957895442 0.1827 4026137797693549}
//[GeoRadiu] {手工茶 104.07240003347397 30.664120006214254 87.9964 4026137800481654}
//[GeoRadiu] {四川大剧院 104.07437950372696 30.664804380927272 213.3854 4026137806216312}
//[GeoRadiu] {新华文轩 104.0700826048851 30.664649762936556 296.4651 4026137800049783}

georadiusbymember 使用方法

resRadiusByMember, err := GlobalClient.GeoRadiusByMember("geo_hash_test", "天府广场", &redis.GeoRadiusQuery{
        Radius:      800,
        Unit:        "m",
        WithCoord:   true,
        WithDist:    true,
        WithGeoHash: true,
        Count:       4,
        Sort:        "ASC",
    }).Result()

//输出
//[GeoRadiusByMember] {天府广场 104.07283455133438 30.663422957895442 0 4026137797693549}
//[GeoRadiusByMember] {手工茶 104.07240003347397 30.664120006214254 87.9725 4026137800481654}
//[GeoRadiusByMember] {四川大剧院 104.07437950372696 30.664804380927272 213.2058 4026137806216312}
//[GeoRadiusByMember] {新华文轩 104.0700826048851 30.664649762936556 296.5478 4026137800049783}

结语

完整代码地址

联系 QQ: 3355168235

上一篇下一篇

猜你喜欢

热点阅读