带小白入门

GEO一般情况下是存储地理位置的(反正我没有用GEO存储过其他东西存储)

###首先是版本问题:

###redis的GEO是在3.2以后才出的,Win10客户端在3.2以上的版本并不多,如果有服务器的话liunx的版本比较多也比较高,但并不是没有

安装链接:

https://github.com/microsoftarchive/redis/releases

下载zip文件解压就行了

其他人的安装教程

Windows 安装Redis(图文详解)_redis windows_hlsongsong的博客-CSDN博客

--------------------------------------------------------------

pom文件同样也需要3.2版本以上的

 我遇见的都知道redis有GEO并不知道版本就去试pom有GEO但是自己学习的时候用的老版本的redis没有,百度也找不到正确的报错原因。。。。。。。。版本报错说的好像是bean注入的问题

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <!-- redis -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

--------------------------------------------------------------

下面是代码实现

@RequestMapping("CreateOrder")
public AjaxResult Login(@RequestBody Orders orders) throws ParseException {
    //------------------------------------------------------------------
    //实现类去处理逻辑
    boolean b= orderService.OrderUserStuId(orders.getUserId());
    if (!b){
        return AjaxResult.success(orders);
    }
    //Stirng不容易丢失精度(也好处理数据)
    String tip = orders.getTip();
    String zip = orders.getZip();
    //---------------------------------上面可以忽略-------------------------------

    //拆分 XY轴
    String[] start = tip.split(" ");
    String[] end = zip.split(" ");
    //这里写地里位置 接受的是double :116.78688,39.22793
    Point point = new Point(Double.parseDouble(start[0]), Double.parseDouble(start[1]));

    //存储地理位置   键:订单id
    redisTemplate.opsForGeo().add("ditu",point,orders.getId()+"");
    //看看存储的值是否可以正常读取
    List position = redisTemplate.opsForGeo().position("ditu", orders.getId()+"");
    System.out.println(position);
    orderService.save(orders);
    return AjaxResult.success(orders);
}



/**
 * 获取附近的订单
 *范围 :50000米
 * @param orderId
 * @return 返回值 :订单id列表
 */
@RequestMapping("/ditu")
public List<Orders> getOrderDitu(@PathParam("orderId") String orderId){
    ArrayList<Integer> list = redisGEOUtils.GetUsers(orderId);
    List<Orders> orders = orderService.listByIds(list);
    return orders;
}
//------------------获取成员父级半径50000米的其他成员------GetUsers方法----------------------------------

// redisTemplate.opsForGeo().radius("key",成员的键,距离);

GeoResults ditu = redisTemplate.opsForGeo().radius("ditu",orderId,50000);

for (Object o : ditu.getContent()) {
    String s = o.toString().replaceAll("GeoResult \\[content: RedisGeoCommands.GeoLocation\\(name=", "");
    String s1 = s.replaceAll(", point=null\\), distance: 0\\.0, ]", "");
    if (s1.equals(orderId) || s1.length() < 18) {
        continue;
    }
    list.add(Integer.parseInt(s1.trim()));
}
//------------------------删除坐标点-----DeleteOrderId方法----------------------------------------------------

/**
 * 根据OrderId删除
 * @param orderId
 * @return
 */
public Long DeleteOrderId(String orderId){
    Long ditu = redisTemplate.opsForGeo().remove("ditu", orderId);
    return ditu;
}

实体类直接CV

public class Orders {
    private long id;

    private String time;

    private String price;

    private Date date;

    private String weather;
    private String tip;
    private String tipName;
    private String zip;
    private String zipName;

    private Integer status;

    private Long userId;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }

    public String getTip() {
        return tip;
    }

    public void setTip(String tip) {
        this.tip = tip;
    }

    public String getTipName() {
        return tipName;
    }

    public void setTipName(String tipName) {
        this.tipName = tipName;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public String getZipName() {
        return zipName;
    }

    public void setZipName(String zipName) {
        this.zipName = zipName;
    }


    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    @Override
    public String toString() {
        return "Orders{" +
                "id=" + id +
                ", time='" + time + '\'' +
                ", price='" + price + '\'' +
                ", date=" + date +
                ", weather='" + weather + '\'' +
                ", tip='" + tip + '\'' +
                ", tipName='" + tipName + '\'' +
                ", zip='" + zip + '\'' +
                ", zipName='" + zipName + '\'' +
                ", status=" + status +
                ", userId=" + userId +
                '}';
    }
}

下面是redis扩展这里推荐去看看其他文章了这里只是告诉你怎么在java中使用

在redis里GEO的指令

  • geoadd:添加地理位置的坐标。
  • geopos:获取地理位置的坐标。
  • geodist:计算两个位置之间的距离。
  • georadius:根据用户给定的经纬度坐标来获取指定范围内的地理位置集合。
  • georadiusbymember:根据储存在位置集合里面的某个地点获取指定范围内的地理位置集合。
  • geohash:返回一个或多个位置对象的 geohash 值。

redis推荐看看这个文章:

Redis的geo的相关命令_redis opsforgeo常用指令_北海冥鱼未眠的博客-CSDN博客

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐