美文网首页
golang 通过 Redis GEO 实现 LBS 功能

golang 通过 Redis GEO 实现 LBS 功能

作者: hwholiday | 来源:发表于2020-03-17 13:22 被阅读0次

    LBS

    • 基于位置的服务(Location Based Services,LBS),是利用各类型的定位技术来获取定位设备当前的所在位置,通过移动互联网向定位设备提供信息资源和基础服务。LBS首先读者可利用定位技术确定自身的空间位置,随后读者便可通过移动互联网来获取与位置相关资源和信息。LBS服务中融合了移动通讯、互联网络、空间定位、位置信息、大数据等多种信息技术,利用移动互联网络服务平台进行数据更新和交互,使用户可以通过空间定位来获取相应的服务。

    redis 中关于 GEO 的方法

    • geoadd:增加某个位置的坐标。
    • geopos:获取某个位置的坐标。
    • geohash:获取某个位置的geohash值。
    • geodist:获取两个位置的距离。
    • georadius:根据给定位置坐标获取指定范围内的位置集合。
    • georadiusbymember:根据给定位置获取指定范围内的位置集合。

    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}
    
    

    结语

    • 使用LBS我们可以很方便的编写关于附近相关的业务逻辑
    • 欢迎添加QQ一起讨论

    完整代码地址

    联系 QQ: 3355168235

    相关文章

      网友评论

          本文标题:golang 通过 Redis GEO 实现 LBS 功能

          本文链接:https://www.haomeiwen.com/subject/tthtyhtx.html