目录
- 三、javaapi
前言
redis自3.2版本开始提供了geo(geograph)功能,支持地理位置相关操作,以实现诸如附近的人这类依赖于地理位置信息的功能。
工具
百度经纬度拾取器
一、测试数据
120.70012 28.00135 温州
120.207686 30.253359 杭州
121.482537 31.238034 上海
118.793117 32.068407 南京
二、基本命令
1. geoadd
为了进行地理位置相关操作,我们首先需要将具体的地理位置记录起来,可以通过执行geoadd 命令来完成 命令格式如下
geoadd 集合名称 经度 纬度 名称 [longitude latitude name...] //添加集合 geoadd citys 120.70012 28.00135 wenzhou 120.207686 30.253359 hanghzou
查看已添加集合
2.geopos
此命令根据输入的位置名称获取位置的信息坐标,语法如下
geopos 集合名称 位置 [name...] geopos address wuyue
查看坐标信息
3.geodist
此命令用于计算两个位置之间的距离,语法如下
geodist 集合名称 位置1 位置2 [unit] //计算杭州到南京之间的距离 geodist citys hanghzredis:0>ou nanjing km
可选参数:unit用于指定计算距离时的单位,他的值可以是以下单位的其中一个
m :表示米
km:表示千米
mi:表示英里
ft:表示英尺。
4.georadius
georadius使用用户给定的经纬度作为计算范围时的中心点,
georadius 集合名称 精度 纬度 radius m|km|ft|mi| [withcoord] [withdist] [asc|desc] [count count] //查询据我100km之内的城市 georadius citys 120.754274 27.983296 100 km
radius:距离
withcoord:返回坐标
由于版本原因可能为空
withdist:同时返回距离
asc|desc:排序
count:取多少长度
5. georadiusbymember
georadiusbymember使用存储在位置集合里的某个地点作为中心点
georadiusbymember 地址集合 地点名称 距离 单位 //查询距离wuyue五公里之内的地点 georadiusbymember address wuyue 5 km
三、javaapi
实体类
package com.jiale.web.controller.pojo;
import lombok.data;
import java.io.serializable;
/**
* @author: hello world
* @date: 2021/9/16 16:12
* @description:
*/
@data
public class addressinfo implements serializable {
/**网点名称*/
private string title;
/**网点地址*/
private string address;
/**网点联系方式*/
private string phone;
/**网点坐标-精度*/
private double x;
/**网点坐标-纬度*/
private double y;
}
package com.jiale;
import com.jiale.common.config.jialeglobal;
import com.jiale.web.controller.pojo.addressinfo;
import org.junit.jupiter.api.test;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.redisgeocommands;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.web.bind.annotation.requestmapping;
import java.math.bigdecimal;
import java.util.list;
/**
* @author: hello world
* @date: 2021/9/15 17:47
* @description:
*/
@springboottest
public class jialetests {
@autowired
redistemplate redistemplate;
@test
public void test1(){
system.out.println(jialeglobal.getuploadpath());
}
/**
* 一.向redis中添加位置信息
* 1.网店位置信息-geo
* 2.网点详细信息
*/
@test
public void geoadd(){
//1.网点位置信息存储120.653208,28.032606
point point = new point(120.653208, 28.032606);
redistemplate.boundgeoops("outlets").add(point,"江心屿");
//2.网点详细信息
addressinfo addressinfo = new addressinfo();
addressinfo.settitle("江心屿");
addressinfo.setaddress("浙江省温州市鹿城区江心屿景区内");
addressinfo.setphone("(0577)88201281");
addressinfo.setx(120.653208);
addressinfo.sety(28.032606);
redistemplate.boundhashops("outletsinfo").put("江心屿",addressinfo);
}
/**
* 二.获取位置坐标信息
*/
@test
public void geopos(){
//传入位置名称查询位置信息
list<point> position = redistemplate.boundgeoops("outlets").position("南塘五组团");
for (point point : position) {
system.out.println(point);
}
}
/**
* 三.计算两个位置信息
*/
@test
public void geodist(){
/**
distance distance = redistemplate.boundgeoops("outlets").distance("江心屿", "温州乐园");
//距离
double value = distance.getvalue();
//单位
string unit = distance.getunit();
system.out.println("两点相距:"+value+unit);
*/
//以km展示
/**
distance distance = redistemplate.boundgeoops("outlets").distance("江心屿", "温州乐园", metrics.kilometers);
//距离
double value = distance.getvalue();
//单位
string unit = distance.getunit();
system.out.println("两点相距:"+value+unit);*/
//保留两位小数
distance distance = redistemplate.boundgeoops("outlets").distance("江心屿", "温州乐园", metrics.kilometers);
//距离
double value = distance.getvalue();
//单位
string unit = distance.getunit();
system.out.println("两点相距:"+new bigdecimal(value).setscale(2,bigdecimal.round_half_up) +unit);
}
/**
* 四.按照给定的经纬度查找指定范围的位置
*/
@test
public void georadius(){
//指定位置
point point = new point(120.754274, 27.983296);
//构建条件-10km
distance distance = new distance(10, metrics.kilometers);
circle circle = new circle(point,distance);
//redisgeocommands
redisgeocommands.georadiuscommandargs args = redisgeocommands.georadiuscommandargs.newgeoradiusargs();
//包含位置信息
args.includedistance();
//存放的是查询到的网址位置信息
georesults<redisgeocommands.geolocation<string>> outlets = redistemplate.boundgeoops("outlets").radius(circle, args);
for (georesult<redisgeocommands.geolocation<string>> outlet : outlets) {
//获取距离信息
distance outletdistance = outlet.getdistance();
//距离
double value = outletdistance.getvalue();
//单位
string unit = outletdistance.getunit();
system.out.println("当前坐标距离:"+outlet.getcontent().getname()+value+unit);
}
}
/**
* 五.按照指定元素(必须在集合中存在)查找指定范围位置
*/
@test
public void georadiusbymember(){
//构建条件
distance distance = new distance(10, metrics.kilometers);
//构建条件-包含位置信息
redisgeocommands.georadiuscommandargs args = redisgeocommands.georadiuscommandargs.newgeoradiusargs();
args.includedistance();
//参数 key 构建的条件 其他条件
georesults<redisgeocommands.geolocation<string>> outlets = redistemplate.boundgeoops("outlets").radius("江心屿", distance,args);
for (georesult<redisgeocommands.geolocation<string>> outlet : outlets) {
//获取距离信息
distance outletdistance = outlet.getdistance();
//距离
double value = outletdistance.getvalue();
//单位
string unit = outletdistance.getunit();
system.out.println("江心屿距离:"+outlet.getcontent().getname()+value+unit);
}
}
}
到此这篇关于使用redis实现附近的人的文章就介绍到这了,更多相关redis实现附近的人内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!