利用Redis的Geo功能实现查找附近的位 - 云+社区 - 腾讯云

package com.easy.hotel.pms.util;import com.easy.hotel.common.data.tenant.TenantContextHolder;
import com.easy.hotel.pms.mapper.CommunityMapper;
import com.easy.hotel.pms.service.impl.CommunityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;import java.util.ArrayList;
import java.util.List;/***/
@Component
public class RedisGeoUntil {@Autowiredprivate  RedisTemplate redisTemplate;//communicode+租户号public static String redisKeyCommunityCode = "REDIS_GEO_COMMUNITY_CODE:";//communicode+用户IDpublic static String redisGeoUserCommunity = "REDIS_GEO_USER_COMMUNITY:";public static  String getRedisKey(){//区分租户的Long tenantId = TenantContextHolder.getTenantId();String redisKey =  RedisGeoUntil.redisKeyCommunityCode + tenantId;return   redisKey;}/*新增   修改和新增是一样的*/public void insertRedisGeo(String redisKey , Double longitudeX,Double latitudeY,String code){//增加(添加经纬度信息)//Point的属性值,x轴是经度longitude,y轴是纬度latitude。Point point  = new Point(longitudeX,latitudeY);Long addedNum = redisTemplate.opsForGeo().add(redisKey,point,code);System.out.println(addedNum);}/*删除*/public void deleteRedisGeo(String redisKey , String code){//删除//redisTemplate.opsForZSet().remove(redisKey,"hangzhou ");Long remove = redisTemplate.boundZSetOps(redisKey).remove(code);System.out.println(remove);}/*修改  不用删除    修改和新增是一样的*/public void updateRedisGeo(String redisKey, Double longitudeX,Double latitudeY , String code){//修改(先删除再新增)//deleteRedisGeo(  redisKey ,   code);insertRedisGeo(  redisKey ,   longitudeX,  latitudeY,  code);System.out.println();}/*查询多个*/public List<Point> getRedisGeoList(String redisKey , List<String> codeList) {List<Point> pointList = new ArrayList<>();for(int i = 0 ; i < codeList.size();i++){Point redisGeo = getRedisGeo(redisKey, codeList.get(i));if(redisGeo != null){pointList.add(redisGeo);}}System.out.println(pointList);return pointList;}/*查询单个*/public Point getRedisGeo(String redisKey , String code) {//查询可以查询多个 经纬度List<Point> pointsAll = redisTemplate.opsForGeo().position(redisKey,code);System.out.println(pointsAll);if(!CollectionUtils.isEmpty(pointsAll)){return pointsAll.get(0);}return null;}/*** 计算指定点 5km内的(MaxInteger KM)*/public GeoResults<RedisGeoCommands.GeoLocation<String>> getRedisGeoMaxIntegerKm(String redisKey   , String  code) {Distance disFiveKmFive = new Distance(Integer.MAX_VALUE, Metrics.KILOMETERS);RedisGeoCommands.GeoRadiusCommandArgs argsFiveKmFive = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(Integer.MAX_VALUE);GeoResults<RedisGeoCommands.GeoLocation<String>> resultsFiveKmFive = redisTemplate.opsForGeo().radius(redisKey,code,disFiveKmFive,argsFiveKmFive);System.out.println(resultsFiveKmFive);return  resultsFiveKmFive;}/*** //中心点设置  某个经纬度算距离*/public GeoResults<RedisGeoCommands.GeoLocation<String>> getRedisGeoMaxIntegerKm(String redisKey , Double longitudeX,Double latitudeY ) {//longitude,latitude//中心点设置  某个经纬度算距离Point pointCenter = new Point(longitudeX,latitudeY);Distance distanceCenter = new Distance(Integer.MAX_VALUE, RedisGeoCommands.DistanceUnit.KILOMETERS);Circle circleCenter = new Circle(pointCenter, distanceCenter);RedisGeoCommands.GeoRadiusCommandArgs argsCircle = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(Integer.MAX_VALUE);GeoResults<RedisGeoCommands.GeoLocation<String>> resultsDis = redisTemplate.opsForGeo().radius(redisKey,circleCenter ,argsCircle);System.out.println(resultsDis);return resultsDis;}/*查询单个Hash*/public List<String> getRedisGeoListHash(String redisKey ,  String code) {List<String> resultsHash = redisTemplate.opsForGeo().hash(redisKey,code);System.out.println(resultsHash);return resultsHash;}/*//计算2地之间距离(返回两个地方的距离,可以指定单位,比如米m,千米km,英里mi,英尺ft)*/public Distance getRedisGeoListTwoSpace(String redisKey , String  codeOne,String codeTwo) {Distance disTwoPlace = redisTemplate.opsForGeo().distance(redisKey,codeOne,codeTwo, RedisGeoCommands.DistanceUnit.KILOMETERS);System.out.println(disTwoPlace);return disTwoPlace;}}
package com.easy.hotel.pms.controller;import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.hotel.common.core.constant.enums.REnum;
import com.easy.hotel.common.core.util.EmptyUtils;
import com.easy.hotel.common.core.util.R;
import com.easy.hotel.common.data.mybatis.QPage;
import com.easy.hotel.common.data.mybatis.RPage;
import com.easy.hotel.common.data.tenant.TenantContextHolder;
import com.easy.hotel.common.log.annotation.SysLog;
import com.easy.hotel.common.security.annotation.Inner;import com.easy.hotel.pms.api.dto.house.*;
import com.easy.hotel.pms.api.entity.*;
import com.easy.hotel.pms.api.enums.SiteLevelEnum;
import com.easy.hotel.pms.api.enums.UserTypeEnum;import com.easy.hotel.pms.api.vo.UserHouseVo;
import com.easy.hotel.pms.api.vo.house.*;
import com.easy.hotel.pms.service.*;
import com.easy.hotel.pms.service.impl.CommunityServiceImpl;
import com.easy.hotel.pms.util.RedisGeoUntil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;/*** <p class="detail">* 功能:* </p>** @author cm* @ClassName HouseVo controller.* @Version V1.0.* @date 2019.05.09 14:29:00*/
@RequestMapping("/redisTest")
@RestController
@AllArgsConstructor
@Api(value = "RedisController", tags = "redis房屋Api")
public class RedisGEOController {@Autowiredprivate RedisGeoUntil redisGeoUntil;// 大屏那个先做!!!26号做完  其他的往后延迟一周/*** 获取房屋统计** @param* @return success/false*/@GetMapping("/v2/redisTest")@ApiOperation(value = "redistest获取房屋统计测试-yangjiabin20220412", notes = "获取房屋统计测试-yangjiabin20220412")public R getHouseStatistics() {//Point的属性值,x轴是经度longitude,y轴是纬度latitude。String redisKey =  CommunityServiceImpl.getRedisKey();List<String> codeList = new ArrayList<>();codeList.add("hangzhou1");codeList.add("hangzhou2");codeList.add("hangzhou3");codeList.add("hangzhou4");  // 大屏那个先做!!!26号做完  其他的往后延迟一周codeList.add("hangzhou5");codeList.add("hangzhou6");codeList.add("hangzhou7");codeList.add("hangzhou8");/**/codeList.add("huanlecheng");codeList.add("nanyuan");codeList.add("sd001");codeList.add("tlshehz");codeList.add("bzhhgwjhihe");codeList.add("32414314321");codeList.add("cssq");codeList.add("ertertert");codeList.add("qiaosi");codeList.add("yangjiabinshequ");codeList.add("10011223");codeList.add("xqsq");codeList.add("xqsq001");codeList.add("xqsq002");codeList.add("xqsq003");codeList.add("xqsq004");codeList.add("xqsq005");codeList.add("xqsq11001");codeList.add("xqsq11002");codeList.add("xqsq11003");//查询可以查询多个 经纬度List<Point> redisGeoList0 = redisGeoUntil.getRedisGeoList(redisKey, codeList);//[]//中心点设置  某个经纬度算距离GeoResults<RedisGeoCommands.GeoLocation<String>> redisGeoMaxIntegerKm2 = redisGeoUntil.getRedisGeoMaxIntegerKm(redisKey, 120.206590, 30.233336);GeoResults<RedisGeoCommands.GeoLocation<String>> redisGeoMaxIntegerKm1222 = redisGeoUntil.getRedisGeoMaxIntegerKm(redisKey, "cssq");//增加(添加经纬度信息)redisGeoUntil.insertRedisGeo(redisKey,116.10,19.10,"hangzhou1");redisGeoUntil.insertRedisGeo(redisKey,116.20,19.20,"hangzhou2");
//      redisGeoUntil.insertRedisGeo(redisKey,116.30,19.30,"hangzhou3");
//      redisGeoUntil.insertRedisGeo(redisKey,116.40,19.40,"hangzhou4");
//      redisGeoUntil.insertRedisGeo(redisKey,116.50,19.50,"hangzhou5");
//      redisGeoUntil.insertRedisGeo(redisKey,116.60,19.60,"hangzhou6");//查询可以查询多个 经纬度List<Point> redisGeoList2 = redisGeoUntil.getRedisGeoList(redisKey, codeList);//[Point [x=116.099999, y=19.100000], Point [x=116.199997, y=19.200000], Point [x=116.300001, y=19.299999], Point [x=116.399999, y=19.399999], Point [x=116.500002, y=19.499999], Point [x=116.600001, y=19.599999]]//计算指定点codeGeoResults<RedisGeoCommands.GeoLocation<String>> hangzhou11 = redisGeoUntil.getRedisGeoMaxIntegerKm(redisKey, "hangzhou1");//GeoResults: [averageDistance: 30.5927 KILOMETERS, results: GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou1, point=Point [x=116.099999, y=19.100000]), distance: 0.0 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou2, point=Point [x=116.199997, y=19.200000]), distance: 15.3006 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou3, point=Point [x=116.300001, y=19.299999]), distance: 30.5972 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou4, point=Point [x=116.399999, y=19.399999]), distance: 45.889 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou5, point=Point [x=116.500002, y=19.499999]), distance: 61.1767 KILOMETERS, ]]//删除redisGeoUntil.deleteRedisGeo(redisKey,"hangzhou5");//没事。。。redisGeoUntil.deleteRedisGeo(redisKey,"删除不存在的");//查询可以查询多个 经纬度List<Point> redisGeoList3 = redisGeoUntil.getRedisGeoList(redisKey, codeList);//[Point [x=116.099999, y=19.100000], Point [x=116.199997, y=19.200000], Point [x=116.300001, y=19.299999], Point [x=116.399999, y=19.399999], Point [x=116.600001, y=19.599999]]//修改(和新增一样)redisGeoUntil.updateRedisGeo( redisKey, 116.99,19.99,"hangzhou4");//查询可以查询多个 经纬度List<Point> redisGeoList4 = redisGeoUntil.getRedisGeoList(redisKey, codeList);//[Point [x=116.099999, y=19.100000], Point [x=116.199997, y=19.200000], Point [x=116.300001, y=19.299999], Point [x=116.989999, y=19.990001], Point [x=116.600001, y=19.599999]]//查询单个Point hangzhou1 = redisGeoUntil.getRedisGeo(redisKey, "hangzhou1");//[Point [x=116.099999, y=19.100000]]//查询单个Point hangzhou2 = redisGeoUntil.getRedisGeo(redisKey, "没有这个地址");//[null]//查找一个位置的时间复杂度为 ?? hash值List<String> hangzhou122 = redisGeoUntil.getRedisGeoListHash(redisKey, "hangzhou1");//[we6s9vj3w40]//计算2地之间距离(返回两个地方的距离,可以指定单位,比如米m,千米km,英里mi,英尺ft)Distance redisGeoListTwoSpace = redisGeoUntil.getRedisGeoListTwoSpace(redisKey, "hangzhou1", "hangzhou2");//15.3006 KILOMETERS//longitude,latitude//中心点设置  某个经纬度算距离GeoResults<RedisGeoCommands.GeoLocation<String>> redisGeoMaxIntegerKm = redisGeoUntil.getRedisGeoMaxIntegerKm(redisKey, 126.40, 29.90);//GeoResults: [averageDistance: 1537.1931 KILOMETERS, results: GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou4, point=Point [x=116.989999, y=19.990001]), distance: 1453.0984 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou6, point=Point [x=116.600001, y=19.599999]), distance: 1512.4838 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou3, point=Point [x=116.300001, y=19.299999]), distance: 1558.2093 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou2, point=Point [x=116.199997, y=19.200000]), distance: 1573.4598 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou1, point=Point [x=116.099999, y=19.100000]), distance: 1588.7142 KILOMETERS, ]]//selectRidesPoint(146.40f,49.90f,Integer.MAX_VALUE);//计算指定点 5km内的GeoResults<RedisGeoCommands.GeoLocation<String>> redisGeoMaxIntegerKm12 = redisGeoUntil.getRedisGeoMaxIntegerKm(redisKey, "hangzhou1");//GeoResults: [averageDistance: 51.675599999999996 KILOMETERS, results: GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou1, point=Point [x=116.099999, y=19.100000]), distance: 0.0 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou2, point=Point [x=116.199997, y=19.200000]), distance: 15.3006 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou3, point=Point [x=116.300001, y=19.299999]), distance: 30.5972 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou6, point=Point [x=116.600001, y=19.599999]), distance: 76.4596 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou4, point=Point [x=116.989999, y=19.990001]), distance: 136.0206 KILOMETERS, ]]return  null;}// private Map<String, Map<String, Double>> selectRidesPoint(float lng, float lat, Integer radius) {
//      String redisLnglatName = redisKey;
//      //中心点设置
//      Point point = new Point(lng, lat);
//      Metric metric = RedisGeoCommands.DistanceUnit.KILOMETERS;
//      Distance distance = new Distance(radius, metric);
//      Circle circle = new Circle(point, distance);
//
//      //redis查询参数配置:GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count]
//      RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs
//              .newGeoRadiusArgs()
//              .includeDistance()
//              .includeCoordinates()
//              .sortAscending()
//              .limit(5);
//      GeoResults<RedisGeoCommands.GeoLocation<String>> data = redisTemplate.opsForGeo().geoRadius(redisLnglatName, circle, args);
//      System.out.println("计算circleDis 和 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5 的距离 ");
//      System.out.println(data);
//
//
//
//
//
//
//
//
//
//      //数据封装
//      Map<String, Map<String, Double>> cameraMap = new HashMap<>();
//      if (data != null) {
//          data.forEach(geoLocationGeoResult -> {
//              RedisGeoCommands.GeoLocation<String> content = geoLocationGeoResult.getContent();
//              Map<String, Double> pointMap = new HashMap<>();
//              //member 名称  如  tianjin
//              String name = content.getName();
//              // 对应的经纬度坐标
//              Point pos = content.getPoint();
//              // 距离中心点的距离
//              Distance dis = geoLocationGeoResult.getDistance();
//              pointMap.put("lng", pos.getX());
//              pointMap.put("lat", pos.getY());
//              cameraMap.put(name, pointMap);
//          });
//      }
//      return cameraMap;
//  }}
package com.easy.hotel.pms.util;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;import java.util.ArrayList;
import java.util.List;/***/
@Component
public class RedisGeoUntil {@Autowiredprivate  RedisTemplate redisTemplate;/*新增*/public void insertRedisGeo(String redisKey , Double longitudeX,Double latitudeY,String code){//增加(添加经纬度信息)//Point的属性值,x轴是经度longitude,y轴是纬度latitude。Point point  = new Point(longitudeX,latitudeY);Long addedNum = redisTemplate.opsForGeo().add(redisKey,point,code);System.out.println(addedNum);}/*删除*/public void deleteRedisGeo(String redisKey , String code){//删除//redisTemplate.opsForZSet().remove(redisKey,"hangzhou5");Long remove = redisTemplate.boundZSetOps(redisKey).remove(code);System.out.println(remove);}/*修改*/public void updateRedisGeo(String redisKey, Double longitudeX,Double latitudeY , String code){//修改(先删除再新增)deleteRedisGeo(  redisKey ,   code);insertRedisGeo(  redisKey ,   longitudeX,  latitudeY,  code);System.out.println();}/*查询多个*/public List<Point> getRedisGeoList(String redisKey , List<String> codeList) {List<Point> pointList = new ArrayList<>();for(int i = 0 ; i < codeList.size();i++){Point redisGeo = getRedisGeo(redisKey, codeList.get(i));if(redisGeo != null){pointList.add(redisGeo);}}System.out.println(pointList);return pointList;}/*查询单个*/public Point getRedisGeo(String redisKey , String code) {//查询可以查询多个 经纬度List<Point> pointsAll = redisTemplate.opsForGeo().position(redisKey,code);System.out.println(pointsAll);if(!CollectionUtils.isEmpty(pointsAll)){return pointsAll.get(0);}return null;}/*** 计算指定点 5km内的(MaxInteger KM)*/public GeoResults<RedisGeoCommands.GeoLocation<String>> getRedisGeoMaxIntegerKm(String redisKey   , String  code) {Distance disFiveKmFive = new Distance(Integer.MAX_VALUE, Metrics.KILOMETERS);RedisGeoCommands.GeoRadiusCommandArgs argsFiveKmFive = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);GeoResults<RedisGeoCommands.GeoLocation<String>> resultsFiveKmFive = redisTemplate.opsForGeo().radius(redisKey,code,disFiveKmFive,argsFiveKmFive);System.out.println(resultsFiveKmFive);return  resultsFiveKmFive;}/*** //中心点设置  某个经纬度算距离*/public GeoResults<RedisGeoCommands.GeoLocation<String>> getRedisGeoMaxIntegerKm(String redisKey , Double longitudeX,Double latitudeY ) {//longitude,latitude//中心点设置  某个经纬度算距离Point pointCenter = new Point(longitudeX,latitudeY);Distance distanceCenter = new Distance(Integer.MAX_VALUE, RedisGeoCommands.DistanceUnit.KILOMETERS);Circle circleCenter = new Circle(pointCenter, distanceCenter);RedisGeoCommands.GeoRadiusCommandArgs argsCircle = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);GeoResults<RedisGeoCommands.GeoLocation<String>> resultsDis = redisTemplate.opsForGeo().radius(redisKey,circleCenter ,argsCircle);System.out.println(resultsDis);return resultsDis;}/*查询单个Hash*/public List<String> getRedisGeoListHash(String redisKey ,  String code) {List<String> resultsHash = redisTemplate.opsForGeo().hash(redisKey,code);System.out.println(resultsHash);return resultsHash;}/*//计算2地之间距离(返回两个地方的距离,可以指定单位,比如米m,千米km,英里mi,英尺ft)*/public Distance getRedisGeoListTwoSpace(String redisKey , String  codeOne,String codeTwo) {Distance disTwoPlace = redisTemplate.opsForGeo().distance(redisKey,codeOne,codeTwo, RedisGeoCommands.DistanceUnit.KILOMETERS);System.out.println(disTwoPlace);return disTwoPlace;}}
package com.easy.hotel.pms.controller;import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.hotel.common.core.constant.enums.REnum;
import com.easy.hotel.common.core.util.EmptyUtils;
import com.easy.hotel.common.core.util.R;
import com.easy.hotel.common.data.mybatis.QPage;
import com.easy.hotel.common.data.mybatis.RPage;
import com.easy.hotel.common.data.tenant.TenantContextHolder;
import com.easy.hotel.common.log.annotation.SysLog;
import com.easy.hotel.common.security.annotation.Inner;import com.easy.hotel.pms.api.dto.house.*;
import com.easy.hotel.pms.api.entity.*;
import com.easy.hotel.pms.api.enums.SiteLevelEnum;
import com.easy.hotel.pms.api.enums.UserTypeEnum;import com.easy.hotel.pms.api.vo.UserHouseVo;
import com.easy.hotel.pms.api.vo.house.*;
import com.easy.hotel.pms.service.*;
import com.easy.hotel.pms.util.RedisGeoUntil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;/*** <p class="detail">* 功能:* </p>** @author cm* @ClassName House controller.* @Version V1.0.* @date 2019.05.09 14:29:00*/
@RequestMapping("/redisTest")
@RestController
@AllArgsConstructor
@Api(value = "RedisController", tags = "redis房屋Api")
public class RedisController {private final RedisTemplate redisTemplate;//租户号+communicodeprivate static String redisKeyCommunityCode = "REDIS_GEO_COMMUNITY_CODE:";@Autowiredprivate RedisGeoUntil redisGeoUntil;public  String getRedisKey(){Long tenantId = TenantContextHolder.getTenantId();String redisKey = redisKeyCommunityCode+tenantId;return     redisKey;}/*** 获取房屋统计** @param* @return success/false*/@GetMapping("/v2/redisTest")@ApiOperation(value = "redistest获取房屋统计测试-yangjiabin20220412", notes = "获取房屋统计测试-yangjiabin20220412")public R getHouseStatistics() {//Point的属性值,x轴是经度longitude,y轴是纬度latitude。String redisKey =  getRedisKey();List<String> codeList = new ArrayList<>();codeList.add("hangzhou1");codeList.add("hangzhou2");codeList.add("hangzhou3");codeList.add("hangzhou4");codeList.add("hangzhou5");codeList.add("hangzhou6");codeList.add("hangzhou7");codeList.add("hangzhou8");//查询可以查询多个 经纬度List<Point> redisGeoList0 = redisGeoUntil.getRedisGeoList(redisKey, codeList);//[]//增加(添加经纬度信息)redisGeoUntil.insertRedisGeo(redisKey,116.10,19.10,"hangzhou1");redisGeoUntil.insertRedisGeo(redisKey,116.20,19.20,"hangzhou2");redisGeoUntil.insertRedisGeo(redisKey,116.30,19.30,"hangzhou3");redisGeoUntil.insertRedisGeo(redisKey,116.40,19.40,"hangzhou4");redisGeoUntil.insertRedisGeo(redisKey,116.50,19.50,"hangzhou5");redisGeoUntil.insertRedisGeo(redisKey,116.60,19.60,"hangzhou6");//查询可以查询多个 经纬度List<Point> redisGeoList2 = redisGeoUntil.getRedisGeoList(redisKey, codeList);//[Point [x=116.099999, y=19.100000], Point [x=116.199997, y=19.200000], Point [x=116.300001, y=19.299999], Point [x=116.399999, y=19.399999], Point [x=116.500002, y=19.499999], Point [x=116.600001, y=19.599999]]//计算指定点codeGeoResults<RedisGeoCommands.GeoLocation<String>> hangzhou11 = redisGeoUntil.getRedisGeoMaxIntegerKm(redisKey, "hangzhou1");//GeoResults: [averageDistance: 30.5927 KILOMETERS, results: GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou1, point=Point [x=116.099999, y=19.100000]), distance: 0.0 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou2, point=Point [x=116.199997, y=19.200000]), distance: 15.3006 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou3, point=Point [x=116.300001, y=19.299999]), distance: 30.5972 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou4, point=Point [x=116.399999, y=19.399999]), distance: 45.889 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou5, point=Point [x=116.500002, y=19.499999]), distance: 61.1767 KILOMETERS, ]]//删除redisGeoUntil.deleteRedisGeo(redisKey,"hangzhou5");//查询可以查询多个 经纬度List<Point> redisGeoList3 = redisGeoUntil.getRedisGeoList(redisKey, codeList);//[Point [x=116.099999, y=19.100000], Point [x=116.199997, y=19.200000], Point [x=116.300001, y=19.299999], Point [x=116.399999, y=19.399999], Point [x=116.600001, y=19.599999]]//修改(先删除再新增)redisGeoUntil.updateRedisGeo( redisKey, 116.99,19.99,"hangzhou4");//查询可以查询多个 经纬度List<Point> redisGeoList4 = redisGeoUntil.getRedisGeoList(redisKey, codeList);//[Point [x=116.099999, y=19.100000], Point [x=116.199997, y=19.200000], Point [x=116.300001, y=19.299999], Point [x=116.989999, y=19.990001], Point [x=116.600001, y=19.599999]]//查询单个Point hangzhou1 = redisGeoUntil.getRedisGeo(redisKey, "hangzhou1");//[Point [x=116.099999, y=19.100000]]//查询单个Point hangzhou2 = redisGeoUntil.getRedisGeo(redisKey, "没有这个地址");//[null]//查找一个位置的时间复杂度为 ?? hash值redisGeoUntil.getRedisGeoListHash(redisKey, "hangzhou1");//[we6s9vj3w40]//计算2地之间距离(返回两个地方的距离,可以指定单位,比如米m,千米km,英里mi,英尺ft)Distance redisGeoListTwoSpace = redisGeoUntil.getRedisGeoListTwoSpace(redisKey, "hangzhou1", "hangzhou2");//15.3006 KILOMETERS//longitude,latitude//中心点设置  某个经纬度算距离GeoResults<RedisGeoCommands.GeoLocation<String>> redisGeoMaxIntegerKm = redisGeoUntil.getRedisGeoMaxIntegerKm(redisKey, 126.40, 29.90);//GeoResults: [averageDistance: 1537.1931 KILOMETERS, results: GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou4, point=Point [x=116.989999, y=19.990001]), distance: 1453.0984 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou6, point=Point [x=116.600001, y=19.599999]), distance: 1512.4838 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou3, point=Point [x=116.300001, y=19.299999]), distance: 1558.2093 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou2, point=Point [x=116.199997, y=19.200000]), distance: 1573.4598 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou1, point=Point [x=116.099999, y=19.100000]), distance: 1588.7142 KILOMETERS, ]]//selectRidesPoint(146.40f,49.90f,Integer.MAX_VALUE);//计算指定点 5km内的GeoResults<RedisGeoCommands.GeoLocation<String>> redisGeoMaxIntegerKm2 = redisGeoUntil.getRedisGeoMaxIntegerKm(redisKey, "hangzhou1");//GeoResults: [averageDistance: 51.675599999999996 KILOMETERS, results: GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou1, point=Point [x=116.099999, y=19.100000]), distance: 0.0 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou2, point=Point [x=116.199997, y=19.200000]), distance: 15.3006 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou3, point=Point [x=116.300001, y=19.299999]), distance: 30.5972 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou6, point=Point [x=116.600001, y=19.599999]), distance: 76.4596 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou4, point=Point [x=116.989999, y=19.990001]), distance: 136.0206 KILOMETERS, ]]return  null;}//    private Map<String, Map<String, Double>> selectRidesPoint(float lng, float lat, Integer radius) {
//      String redisLnglatName = redisKey;
//      //中心点设置
//      Point point = new Point(lng, lat);
//      Metric metric = RedisGeoCommands.DistanceUnit.KILOMETERS;
//      Distance distance = new Distance(radius, metric);
//      Circle circle = new Circle(point, distance);
//
//      //redis查询参数配置:GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count]
//      RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs
//              .newGeoRadiusArgs()
//              .includeDistance()
//              .includeCoordinates()
//              .sortAscending()
//              .limit(5);
//      GeoResults<RedisGeoCommands.GeoLocation<String>> data = redisTemplate.opsForGeo().geoRadius(redisLnglatName, circle, args);
//      System.out.println("计算circleDis 和 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5 的距离 ");
//      System.out.println(data);
//
//
//
//
//
//
//
//
//
//      //数据封装
//      Map<String, Map<String, Double>> cameraMap = new HashMap<>();
//      if (data != null) {
//          data.forEach(geoLocationGeoResult -> {
//              RedisGeoCommands.GeoLocation<String> content = geoLocationGeoResult.getContent();
//              Map<String, Double> pointMap = new HashMap<>();
//              //member 名称  如  tianjin
//              String name = content.getName();
//              // 对应的经纬度坐标
//              Point pos = content.getPoint();
//              // 距离中心点的距离
//              Distance dis = geoLocationGeoResult.getDistance();
//              pointMap.put("lng", pos.getX());
//              pointMap.put("lat", pos.getY());
//              cameraMap.put(name, pointMap);
//          });
//      }
//      return cameraMap;
//  }}
@RequestMapping("/redisTest")
@RestController
@AllArgsConstructor
@Api(value = "RedisController", tags = "redis房屋Api")
public class RedisController {private final RedisTemplate redisTemplate;private static String redisKey = "GET_DISTANCE";/*** 获取房屋统计** @param* @return success/false*/@GetMapping("/v2/redisTest")@ApiOperation(value = "redistest获取房屋统计测试-yangjiabin20220412", notes = "获取房屋统计测试-yangjiabin20220412")public R getHouseStatistics() {//Point的属性值,x轴是经度longitude,y轴是纬度latitude。Point point1  = new Point(116.40,19.90);Point point2  = new Point(126.40,29.90);Point point3  = new Point(136.40,39.90);Point point4  = new Point(146.40,49.90);Point point5  = new Point(156.40,59.90);Point point6  = new Point(166.40,69.90);//增加(添加经纬度信息)Long addedNum1 = redisTemplate.opsForGeo().add(redisKey,point1,"hangzhou1");Long addedNum2 = redisTemplate.opsForGeo().add(redisKey,point2,"hangzhou2");Long addedNum3 = redisTemplate.opsForGeo().add(redisKey,point3,"hangzhou3");Long addedNum4 = redisTemplate.opsForGeo().add(redisKey,point4,"hangzhou4");Long addedNum5 = redisTemplate.opsForGeo().add(redisKey,point5,"hangzhou5");//查询可以查询多个 经纬度List<Point> pointsAll = redisTemplate.opsForGeo().position(redisKey,"hangzhou1","hangzhou2","hangzhou3","hangzhou4","hangzhou5");System.out.println("首次新增 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5 ");System.out.println(pointsAll);//计算指定点 5km内的Distance disFiveKmFive = new Distance(Integer.MAX_VALUE,Metrics.KILOMETERS);RedisGeoCommands.GeoRadiusCommandArgs argsFiveKmFive = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);GeoResults<RedisGeoCommands.GeoLocation<String>>  resultsFiveKmFive = redisTemplate.opsForGeo().radius(redisKey,"hangzhou1",disFiveKmFive,argsFiveKmFive);System.out.println("首次计算hangzhou1 和 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5 的距离 ");System.out.println(resultsFiveKmFive);//删除//redisTemplate.opsForZSet().remove(redisKey,"hangzhou5");redisTemplate.boundZSetOps(redisKey).remove("hangzhou5");//查询可以查询多个 经纬度List<Point> pointsAllDelete = redisTemplate.opsForGeo().position(redisKey,"hangzhou1","hangzhou2","hangzhou3","hangzhou4","hangzhou5");System.out.println("删除hangzhou5 后查询 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5   ");System.out.println(pointsAllDelete);//修改(先删除再新增)redisTemplate.opsForZSet().remove(redisKey,"hangzhou4");Long addedNumDel5 = redisTemplate.opsForGeo().add(redisKey,point6,"hangzhou4");//查询可以查询多个 经纬度List<Point> pointsAll2 = redisTemplate.opsForGeo().position(redisKey,"hangzhou1","hangzhou2","hangzhou3","hangzhou4","hangzhou5");System.out.println("修改hangzhou4 后查询 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5   ");System.out.println(pointsAll2);List<Point> points2 = redisTemplate.opsForGeo().position(redisKey,"hangzhou1");System.out.println("查询hangzhou1    ");System.out.println(points2);//查找一个位置的时间复杂度为 ?? hash值List<String> resultsHash = redisTemplate.opsForGeo().hash(redisKey,"hangzhou1","hangzhou2","hangzhou3");System.out.println(" 查询 hangzhou1 , hangzhou2 , hangzhou3 hash值   ");System.out.println(resultsHash);//计算2地之间距离(返回两个地方的距离,可以指定单位,比如米m,千米km,英里mi,英尺ft)Distance disTwoPlace = redisTemplate.opsForGeo().distance(redisKey,"hangzhou1","hangzhou2", RedisGeoCommands.DistanceUnit.KILOMETERS);System.out.println(" 查询 hangzhou1 , hangzhou2 之间距离   ");System.out.println(disTwoPlace);//计算2地之间距离(返回两个地方的距离,可以指定单位,比如米m,千米km,英里mi,英尺ft)        会报错!!!这个怎么办??
//      Distance disTwoPlace222 = redisTemplate.opsForGeo().distance(redisKey,"hangzhou1","没有这个地址", RedisGeoCommands.DistanceUnit.KILOMETERS);
//      System.out.println(disTwoPlace222);List<Point> pointsNull = redisTemplate.opsForGeo().position(redisKey,"没有这个地址");System.out.println(" 查询 没有的code   ");System.out.println(pointsNull);//longitude,latitude//中心点设置  某个经纬度算距离Point pointCenter = new Point(126.40,29.90);Distance distanceCenter = new Distance(Integer.MAX_VALUE, RedisGeoCommands.DistanceUnit.KILOMETERS);Circle circleCenter = new Circle(pointCenter, distanceCenter);RedisGeoCommands.GeoRadiusCommandArgs argsCircle = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);GeoResults<RedisGeoCommands.GeoLocation<String>> resultsDis = redisTemplate.opsForGeo().radius(redisKey,circleCenter ,argsCircle);//GeoResults<RedisGeoCommands.GeoLocation<String>> resultsDis = redisTemplate.opsForGeo().radius(redisKey,circleDis,argsCircle);System.out.println("计算circleDis 和 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5 的距离 ");System.out.println(resultsDis);selectRidesPoint(146.40f,49.90f,Integer.MAX_VALUE);//计算指定点 5km内的Distance disFiveKm = new Distance(Integer.MAX_VALUE,Metrics.KILOMETERS);RedisGeoCommands.GeoRadiusCommandArgs argsFiveKm = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);GeoResults<RedisGeoCommands.GeoLocation<String>>  resultsFiveKm = redisTemplate.opsForGeo().radius(redisKey,"hangzhou1",disFiveKm,argsFiveKm);System.out.println("计算hangzhou1 和 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5 的距离 ");System.out.println(resultsFiveKm);/*
[Point [x=116.399999, y=19.900001], Point [x=126.399998, y=29.900000], Point [x=136.399998, y=39.900000], Point [x=146.400003, y=49.900000], Point [x=156.400002, y=59.899999]]
GeoResults: [averageDistance: 2828.28654 KILOMETERS, results: GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou1, point=Point [x=116.399999, y=19.900001]), distance: 0.0 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou2, point=Point [x=126.399998, y=29.900000]), distance: 1500.0764 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou3, point=Point [x=136.399998, y=39.900000]), distance: 2929.5193 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou4, point=Point [x=146.400003, y=49.900000]), distance: 4258.88 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou5, point=Point [x=156.400002, y=59.899999]), distance: 5452.957 KILOMETERS, ]]
[Point [x=116.399999, y=19.900001], Point [x=126.399998, y=29.900000], Point [x=136.399998, y=39.900000], Point [x=146.400003, y=49.900000], null]
[Point [x=116.399999, y=19.900001], Point [x=126.399998, y=29.900000], Point [x=136.399998, y=39.900000], Point [x=166.400001, y=69.899999], null]
[Point [x=116.399999, y=19.900001]]
[wedc0wxvut0, wv3fh71sjk0, xp0fzze6q70]
1500.0764 KILOMETERS
[null]
GeoResults: [averageDistance: 0.0 METERS, results: ]
GeoResults: [averageDistance: 2724.986925 KILOMETERS, results: GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou1, point=Point [x=116.399999, y=19.900001]), distance: 0.0 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou2, point=Point [x=126.399998, y=29.900000]), distance: 1500.0764 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou3, point=Point [x=136.399998, y=39.900000]), distance: 2929.5193 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou4, point=Point [x=166.400001, y=69.899999]), distance: 6470.352 KILOMETERS, ]]*/
/*
首次新增 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5
[Point [x=116.399999, y=19.900001], Point [x=126.399998, y=29.900000], Point [x=136.399998, y=39.900000], Point [x=146.400003, y=49.900000], Point [x=156.400002, y=59.899999]]
首次计算hangzhou1 和 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5 的距离
GeoResults: [averageDistance: 2828.28654 KILOMETERS, results: GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou1, point=Point [x=116.399999, y=19.900001]), distance: 0.0 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou2, point=Point [x=126.399998, y=29.900000]), distance: 1500.0764 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou3, point=Point [x=136.399998, y=39.900000]), distance: 2929.5193 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou4, point=Point [x=146.400003, y=49.900000]), distance: 4258.88 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou5, point=Point [x=156.400002, y=59.899999]), distance: 5452.957 KILOMETERS, ]]
删除hangzhou5 后查询 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5
[Point [x=116.399999, y=19.900001], Point [x=126.399998, y=29.900000], Point [x=136.399998, y=39.900000], Point [x=146.400003, y=49.900000], null]
修改hangzhou4 后查询 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5
[Point [x=116.399999, y=19.900001], Point [x=126.399998, y=29.900000], Point [x=136.399998, y=39.900000], Point [x=166.400001, y=69.899999], null]
查询hangzhou1
[Point [x=116.399999, y=19.900001]]查询 hangzhou1 , hangzhou2 , hangzhou3 hash值
[wedc0wxvut0, wv3fh71sjk0, xp0fzze6q70]查询 hangzhou1 , hangzhou2 之间距离
1500.0764 KILOMETERS查询 没有的code
[null]查询 hangzhou1
GeoResults: [averageDistance: 0.0 METERS, results: ]
首次计算hangzhou1 和 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5 的距离
GeoResults: [averageDistance: 2724.986925 KILOMETERS, results: GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou1, point=Point [x=116.399999, y=19.900001]), distance: 0.0 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou2, point=Point [x=126.399998, y=29.900000]), distance: 1500.0764 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou3, point=Point [x=136.399998, y=39.900000]), distance: 2929.5193 KILOMETERS, ],GeoResult [content: RedisGeoCommands.GeoLocation(name=hangzhou4, point=Point [x=166.400001, y=69.899999]), distance: 6470.352 KILOMETERS, ]]*/return  null;}private Map<String, Map<String, Double>> selectRidesPoint(float lng, float lat, Integer radius) {String redisLnglatName = redisKey;//中心点设置Point point = new Point(lng, lat);Metric metric = RedisGeoCommands.DistanceUnit.KILOMETERS;Distance distance = new Distance(radius, metric);Circle circle = new Circle(point, distance);//redis查询参数配置:GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count]RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);GeoResults<RedisGeoCommands.GeoLocation<String>> data = redisTemplate.opsForGeo().geoRadius(redisLnglatName, circle, args);System.out.println("计算circleDis 和 hangzhou1 , hangzhou2 , hangzhou3 , hangzhou4 , hangzhou5 的距离 ");System.out.println(data);//数据封装Map<String, Map<String, Double>> cameraMap = new HashMap<>();if (data != null) {data.forEach(geoLocationGeoResult -> {RedisGeoCommands.GeoLocation<String> content = geoLocationGeoResult.getContent();Map<String, Double> pointMap = new HashMap<>();//member 名称  如  tianjinString name = content.getName();// 对应的经纬度坐标Point pos = content.getPoint();// 距离中心点的距离Distance dis = geoLocationGeoResult.getDistance();pointMap.put("lng", pos.getX());pointMap.put("lat", pos.getY());cameraMap.put(name, pointMap);});}return cameraMap;}}

redis geo 删除函数

做附近的人功能时候,需要用到geo功能,redis正好提供了这个功能,但是发现没有删除geo位置的函数

  • GEOADD 添加一个经纬度到一个集合的位置
  • GEODIST 计算一个集合中2个位置的距离
  • GEOHASH 把经纬度计算成一个标准的Geohash
  • GEOPOS 返回一个集合中指定位置的经纬度
  • GEORADIUS 以一个经纬度为中心 计算指定距离范围内所有的位置元素
  • GEORADIUSBYMEMBER 以一个位置为中心 计算指定距离范围内所有的位置元素

我们发现,上面列出的所有geo相关的函数中没有关于删除geo的函数,怎么才能删除geo位置呢?答案是使用集合函数中的 zrem 即可删除,因为geo本质是储存在了一个集合中的。

对于如何删除redis中geo存入的坐标

redis中geo 就是将坐标存在reids 中可以进行判断坐标之间的距离,判断坐标及公里内的坐标。
但是由于geo大部分博客都是添加geo 进行判断,但是没有教大家怎么删除,对于坐标点比较多的话会
再redis中进行冗余数据。
    geo其实就是redis中的有序集合,大家可以自行百度查看用法。
    我用的是 Map<String, Point> memberCoordinateMap = new HashMap<String, Point>(); 
来存储坐标的RedisTemplate.opsForGeo().geoAdd(b, memberCoordinateMap);怎么运用就看大家
的需求了。b 是用的uuid 当作key值的,
    结束之后我们进行删除geo 。RedisTemplate.opsForZset.remove(b,String.valueof(ccc)),
其中c 就是memberCoordinateMap的key。 需要遍历删除。即可把获赠个有序集合删除

利用Redis的GEO功能,实现位置查询_juejiang坚持的博客-CSDN博客_geo redis 实现

利用Redis的GEO功能,实现位置查询

利用Redis的GEO功能,实现位置查询

juejiang坚持

于 2021-02-24 11:19:56 发布

522
 收藏 1
分类专栏: Java 数据库 文章标签: redis java
版权

Java
同时被 2 个专栏收录
12 篇文章0 订阅
订阅专栏

数据库
3 篇文章0 订阅
订阅专栏
业务需求:项目中有需求要根据图上绘制的路径搜索某半径范围内所有的设备。
1
解决思路:利用高德地图绘制的路线,可以取得路线上所有点位的经纬度。根据Redis的GEO功能,将所有设备的经纬度写入Redis,然后根据点位经纬度和搜索半径,通过Redis提供的方法即可查询出在此范围内的所有设备。

1、Redis Geo提供了6个命令:
GEOADD、GEODIST、GEOPOS、GEOHASH、GEORADIUS、GEORADIUSBYMEMBER
(1)GEOADD key longitude latitude member [longitude latitude member …]
将一个包含经度、纬度、名称的位置存放在key里
(2)GEODIST key member1 member2 [unit]
计算key里指定的两个位置之间的距离,unit单位:m/Km/mi/ft
(3)GEOPOS key member [member …]
返回key里指定位置的经纬度。返回值是一个数组, 数组中的每个项都由两个元素组成: 第一个元素为给定位置元素的经度, 第二个元素则为给定位置元素的纬度。
(4)GEOHASH key member [member …]
返回key里指定位置的 Geohash 表示。返回一个数组, 数组的每个项都是一个geohash表达式 。返回的geohash的位置与指定位置的位置一一对应。
(5)GEORADIUS key longitude latitude radius m/km/ft/mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC/DESC]
以给定的经纬度为中心点,查找指定半径内,包含的所有位置元素。
WITHCOORD: 将位置元素的经度和维度一并返回。
WITHDIST: 位置元素与中心点之间的距离
WITHHASH: 返回位置的 Geohash 表达式
COUNT: 返回指定条数,相当于MySQL查询的limit
ASC/DESC: 根据中心的位置, 按照从近到远或从远到近的方式返回位置元素。
(6)GEORADIUSBYMEMBER key member radius m/km/ft/mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC/DESC]
和GEORADIUS命令操作相同,只是GEORADIUSBYMEMBER的中心点是key里的位置元素。

2、项目中的实现
(1)添加点位到Redis

private void insertRedis(double lng, double lat, String id){
    Map<String, Point> points = new HashMap<>();
    points.put(id, new Point(lng, lat));
    // 添加 Geo
    stringRedisTemplate.boundGeoOps(Constans.REDIS_CAMERA_LNGLAT).geoAdd(points);
}
1
2
3
4
5
6
(2)将一个点位信息删除

private void deleteRedis(String id) {
    stringRedisTemplate.boundZSetOps(Constans.REDIS_CAMERA_LNGLAT).remove(id);
}
1
2
3
(3)根据页面上绘制的路径得到的点位经纬度查询半径范围内的所有数据

private List<String> selectRidesPoint(float lng, float lat, Integer radius) {
    String redisLnglatName = Constans.REDIS_CAMERA_LNGLAT;
    //中心点设置
    Point point = new Point(lng, lat);
    Metric metric = RedisGeoCommands.DistanceUnit.METERS;
    Distance distance = new Distance(radius, metric);
    Circle circle = new Circle(point, distance);

//redis查询参数配置:GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count]
    RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs
        .newGeoRadiusArgs()
        .includeDistance()
        .includeCoordinates()
        .sortAscending()
        .limit(limit);
    GeoResults<RedisGeoCommands.GeoLocation<String>> data = stringRedisTemplate.opsForGeo().geoRadius(redisLnglatName, circle, args);
    
    //数据封装
    Map<String, Map<String, Double>> cameraMap = new HashMap<>();
    if (data != null) {
        data.forEach(geoLocationGeoResult -> {
            RedisGeoCommands.GeoLocation<String> content = geoLocationGeoResult.getContent();
            Map<String, Double> pointMap = new HashMap<>();
            //member 名称  如  tianjin 
            String name = content.getName();
             // 对应的经纬度坐标
            Point pos = content.getPoint();
            // 距离中心点的距离
            Distance dis = geoLocationGeoResult.getDistance();
            pointMap.put("lng", pos.getX());
            pointMap.put("lat", pos.getY());
            cameraMap.put(name, pointMap);
        });
    }
    return cameraMap;
}

附:关于地球经纬度之间间距计算

/**
 * 根据经纬度和半径计算经纬度范围
 *
 * @param raidus 单位米
 * @return minLat, minLng, maxLat, maxLng
 */
private Map<String, Double> getAround(double lat, double lon, int raidus) {

Double latitude = lat;
    Double longitude = lon;

Double degree = (24901 * 1609) / 360.0;
    double raidusMile = raidus;

Double dpmLat = 1 / degree;
    Double radiusLat = dpmLat * raidusMile;
    Double minLat = latitude - radiusLat;
    Double maxLat = latitude + radiusLat;

Double mpdLng = degree * Math.cos(latitude * (Math.PI / 180));
    Double dpmLng = 1 / mpdLng;
    Double radiusLng = dpmLng * raidusMile;
    Double minLng = longitude - radiusLng;
    Double maxLng = longitude + radiusLng;
//        return new double[]{minLat, minLng, maxLat, maxLng};
    
    Map<String, Double> aroundMap = new HashMap<>();
    aroundMap.put("minLng", minLng);
    aroundMap.put("minLat", minLat);
    aroundMap.put("maxLng", maxLng);
    aroundMap.put("maxLat", maxLat);
    return aroundMap;
}
/**
 * 计算地球上任意两点(经纬度)距离
 *
 * @param long1 第一点经度
 * @param lat1  第一点纬度
 * @param long2 第二点经度
 * @param lat2  第二点纬度
 * @return 返回距离 单位:米
 */
private double distanceByLongNLat(double long1, double lat1, double long2, double lat2) {
    double a, b, R;
    R = 6378137;//地球半径
    lat1 = lat1 * Math.PI / 180.0;
    lat2 = lat2 * Math.PI / 180.0;
    a = lat1 - lat2;
    b = (long1 - long2) * Math.PI / 180.0;
    double d;
    double sa2, sb2;
    sa2 = Math.sin(a / 2.0);
    sb2 = Math.sin(b / 2.0);
    d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
    return d;
}
————————————————
版权声明:本文为CSDN博主「juejiang坚持」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/juejiangjianchi/article/details/109205198

利用Redis的Geo功能实现查找附近的位 - 云+社区 - 腾讯云

利用Redis的Geo功能实现查找附近的位

2020-06-28阅读 3880

1. 前言

老板突然要上线一个需求,获取当前位置方圆一公里的业务代理点。明天上线!当接到这个需求的时候我差点吐血,这时间也太紧张了。赶紧去查相关的技术选型。经过一番折腾,终于在晚上十点完成了这个需求。现在把大致实现的思路总结一下。

图1

2. MySQL 不合适

遇到需求,首先要想到现有的东西能不能满足,成本如何。

MySQL是我首先能够想到的,毕竟大部分数据要持久化到MySQL。但是使用MySQL需要自行计算Geohash。需要使用大量数学几何计算,并且需要学习地理相关知识,门槛较高,短时间内不可能完成需求,而且长期来看这也不是MySQL擅长的领域,所以没有考虑它。

Geohash 参考 https://www.cnblogs.com/LBSer/p/3310455.html

2. Redis 中的 GEO

Redis是我们最为熟悉的K-V数据库,它常被拿来作为高性能的缓存数据库来使用,大部分项目都会用到它。从3.2版本开始它开始提供了GEO能力,用来实现诸如附近位置、计算距离等这类依赖于地理位置信息的功能。GEO相关的命令如下:

Redis 命令

描述

GEOHASH

返回一个或多个位置元素的 Geohash 表示

GEOPOS

从 key 里返回所有给定位置元素的位置(经度和纬度)

GEODIST

返回两个给定位置之间的距离

GEORADIUS

以给定的经纬度为中心, 找出某一半径内的元素

GEOADD

将指定的地理空间位置(纬度、经度、名称)添加到指定的 key 中

GEORADIUSBYMEMBER

找出位于指定范围内的元素,中心点是由给定的位置元素决定

Redis 会假设地球为完美的球形, 所以可能有一些位置计算偏差,据说<=0.5%,对于有严格地理位置要求的需求来说要经过一些场景测试来检验是否能够满足需求。

2.1 写入地理信息

那么如何实现目标单位半径内的所有元素呢?我们可以将所有的位置的经纬度通过上表中的GEOADD将这些地理信息转换为 52 位的Geohash写入Redis

该命令格式:

geoadd key longitude latitude member [longitude latitude member ...]

复制

对应例子:

redis> geoadd cities:locs 117.12 39.08 tianjin 114.29 38.02  shijiazhuang
(integer) 2

复制

意思是将经度为117.12纬度为39.08的地点tianjin和经度为114.29纬度为38.02的地点shijiazhuang加入keycities:locssorted set集合中。可以添加一到多个位置。然后我们就可以借助于其他命令来进行地理位置的计算了。

有效的经度从-180 度到 180 度。有效的纬度从-85.05112878 度到 85.05112878 度。当坐标位置超出上述指定范围时,该命令将会返回一个错误。

2.2 统计单位半径内的地区

我们可以借助于GEORADIUS来找出以给定经纬度,某一半径内的所有元素。

该命令格式:

georadius key longtitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC]

复制

这个命令比GEOADD要复杂一些:

  • radius 半径长度,必选项。后面的mkmftmi、是长度单位选项,四选一。
  • WITHCOORD 将位置元素的经度和维度也一并返回,非必选。
  • WITHDIST 在返回位置元素的同时, 将位置元素与中心点的距离也一并返回。距离的单位和查询单位一致,非必选。
  • WITHHASH 返回位置的 52 位精度的Geohash值,非必选。这个我反正很少用,可能其它一些偏向底层的LBS应用服务需要这个。
  • COUNT 返回符合条件的位置元素的数量,非必选。比如返回前 10 个,以避免出现符合的结果太多而出现性能问题。
  • ASC|DESC 排序方式,非必选。默认情况下返回未排序,但是大多数我们需要进行排序。参照中心位置,从近到远使用ASC ,从远到近使用DESC

例如,我们在 cities:locs 中查找以(115.03,38.44)为中心,方圆200km的城市,结果包含城市名称、对应的坐标和距离中心点的距离(km),并按照从近到远排列。命令如下:

redis> georadius cities:locs 115.03 38.44 200 km WITHCOORD WITHDIST ASC
1) 1) "shijiazhuang"2) "79.7653"3) 1) "114.29000169038772583"2) "38.01999994251037407"
2) 1) "tianjin"2) "186.6937"3) 1) "117.02000230550765991"2) "39.0800000535766543"

复制

你可以加上 COUNT 1来查找最近的一个位置。

3. 基于 Redis GEO 实战

大致的原理思路说完了,接下来就是实操了。结合Spring Boot应用我们应该如何做?

3.1 开发环境

需要具有GEO特性的Redis版本,这里我使用的是Redis 4 。另外我们客户端使用 spring-boot-starter-data-redis 。这里我们会使用到 RedisTemplate对象。

3.2 批量添加位置信息

第一步,我们需要将位置数据初始化到Redis中。在Spring Data Redis中一个位置坐标(lng,lat) 可以封装到org.springframework.data.geo.Point对象中。然后指定一个名称,就组成了一个位置Geo信息。RedisTemplate提供了批量添加位置信息的方法。我们可以将章节 2.1中的添加命令转换为下面的代码:

   Map<String, Point> points = new HashMap<>();points.put("tianjin", new Point(117.12, 39.08));points.put("shijiazhuang", new Point(114.29, 38.02));// RedisTemplate 批量添加 GeoredisTemplate.boundGeoOps("cities:locs").add(points);

复制

可以结合Spring Boot 提供的 ApplicationRunner 接口来实现初始化。

@Bean
public ApplicationRunner cacheActiveAppRunner(RedisTemplate<String, String> redisTemplate) {return args -> {final String GEO_KEY = "cities:locs";// 清理缓存redisTemplate.delete(GEO_KEY);Map<String, Point> points = new HashMap<>();points.put("tianjin", new Point(117.12, 39.08));points.put("shijiazhuang", new Point(114.29, 38.02));// RedisTemplate 批量添加 GeoLocationBoundGeoOperations<String, String> geoOps = redisTemplate.boundGeoOps(GEO_KEY);geoOps.add(points);};
}

复制

3.3 查询附近的特定位置

RedisTemplate 针对GEORADIUS命令也有封装:

GeoResults<GeoLocation<M>> radius(K key, Circle within, GeoRadiusCommandArgs args)

复制

Circle对象是封装覆盖的面积(图 1),需要的要素为中心点坐标Point对象、半径(radius)、计量单位(metric), 例如:

Point point = new Point(115.03, 38.44);Metric metric = RedisGeoCommands.DistanceUnit.KILOMETERS;
Distance distance = new Distance(200, metric);Circle circle = new Circle(point, distance);

复制

GeoRadiusCommandArgs用来封装GEORADIUS的一些可选命令参数,参见章节 2.2中的WITHCOORDCOUNTASC等,例如我们需要在返回结果中包含坐标、中心距离、由近到远排序的前 5 条数据:

RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(limit);

复制

然后执行 radius方法就会拿到GeoResults<RedisGeoCommands.GeoLocation<String>>封装的结果,我们对这个可迭代对象进行解析就可以拿到我们想要的数据:

GeoResults<RedisGeoCommands.GeoLocation<String>> radius = redisTemplate.opsForGeo().radius(GEO_STAGE, circle, args);if (radius != null) {List<StageDTO> stageDTOS = new ArrayList<>();radius.forEach(geoLocationGeoResult -> {RedisGeoCommands.GeoLocation<String> content = geoLocationGeoResult.getContent();//member 名称  如  tianjinString name = content.getName();// 对应的经纬度坐标Point pos = content.getPoint();// 距离中心点的距离Distance dis = geoLocationGeoResult.getDistance();});
}

复制

3.4 删除元素

有时候我们可能需要删除某个位置元素,但是RedisGeo并没有删除成员的命令。不过由于它的底层是zset,我们可以借助zrem命令进行删除,对应的Java代码为:

redisTemplate.boundZSetOps(GEO_STAGE).remove("tianjin");

复制

4. 总结

今天我们使用RedisGeo特性实现了常见的附近的地理信息查询需求,简单易上手。其实使用另一个Nosql数据库MongoDB也可以实现。在数据量比较小的情况下Redis已经能很好的满足需要。如果数据量大可使用MongoDB来实现。

RedisGeo实现增删改查 java计算指定坐标最近的距离并排序相关推荐

  1. 基础的java增删改查,Java基础系列(基础):Java使用Cookie增删改查操作!

    什么是Cookie? Cookie是由W3C组织提出,最早由NetScape社区发展的一种机制. Cookie是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个co ...

  2. java 学生信息的增删改查_学生信息的增删改查(java)

    学生信息的增删改查 注意:此处用到的是access数据库. 运行后效果如图: 代码如下: package example; import java.awt.*; import java.awt.eve ...

  3. salesforce 零基础学习(五十一)使用 Salesforce.com SOAP API 实现用户登录以及简单的增删改查(JAVA访问salesforce)...

    此篇请参看:https://resources.docs.salesforce.com/202/latest/en-us/sfdc/pdf/salesforce_developer_environme ...

  4. Web课程设计-仿当当网-增删改查-java+jsp+mysql-期末大作业

    仿当当网web课程设计-jsp+java+mysql-mvc-期末大作业 Web课程设计 一.系统简介 二.系统开发及运行环境 三.系统设计 四.系统实现 Web课程设计 这篇文章是为了记录自己在we ...

  5. 增删改查java代码_程序员:听说你正在为天天写增删改查代码而烦恼

    有相当多的一部分程序员一直都只接触业余应用的功能,天天写业务代码的程序员也被戏称为CURD程序员,CURD就是增(create).改(update).查(read).删(delete)的意思. CUR ...

  6. java连接oracle增删改查,java连接oracle数据库实现增删改查

    package com; import java.sql.*; import java.util.Scanner; public class testOracle { static final Str ...

  7. Jena增删改查java API

    撸了今年阿里.头条和美团的面试,我有一个重要发现.......>>> 插入.更新数据 public static void insert(){String query = " ...

  8. java中的数组增删查改操作,java数组实现增删改查

    java 实现动态数组,Java工具类Arrays中不得不知的常用方法,数组实现队列java,java数组实现增删改查 java 增删改查代码 import java.sql.Connection; ...

  9. java springboot整合zookeeper入门教程(增删改查)

    java springboot整合zookeeper增删改查入门教程 zookeeper的安装与集群搭建参考:https://www.cnblogs.com/zwcry/p/10272506.html ...

最新文章

  1. HTML中的5种空格表示,HTML中的5种空格各表示的意义
  2. PHP小白学习日程之旅
  3. 问题:org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0
  4. Java刷漆问题代码_Java实现蓝桥杯历届试题格子刷油漆
  5. “约见”面试官系列之常见面试题之第七十九篇之ES5和ES6(建议收藏)
  6. Kmplayer播放器 绿色免安装版 2016 中文版
  7. LeetCode MySQL 1581. 进店却未进行过交易的顾客
  8. idea 关联jdk源码_[项目源码]ERP进销存系统
  9. 观察者模式在android网络监控下的运用
  10. (229)DPU加速数据处理
  11. fpga加载程序慢_可重构计算:基于FPGA可重构计算的理论与实践 1.器件架构 译文(一)...
  12. (转)去中心化:关于区块链的争论
  13. 【遥感图像预处理方法】
  14. 力扣 202 快乐数
  15. android 视频录制锐化,从录制到剪辑,用的同款APP,为什么你录制的游戏视频画面会远不如别人?...
  16. 上传大文件报错413问题处理
  17. iOS之KVC原理自定义KVC
  18. 一个古典App开发者的DApp开发之路
  19. 按钮控制LED灯的亮灭
  20. 有哪些有意思的,很 cool 的开源 C++ 项目 ?

热门文章

  1. Spark rdd之sortBy
  2. 朋友千千万,知己有几人。转些短句。
  3. C++使用ffmpeg解码v模块,支持cpu解码、amd64平台的cuda解码和NX平台的Nvmpi解码
  4. Kafka 安装、使用
  5. myeclipse2017 for Mac 破解版本
  6. 撩小姐姐的小程序(二)----旋转3D八音盒
  7. 主成分分析(PCA)数学原理详解
  8. CDA学习笔记--EXCEL篇
  9. android ios 微信 备份通讯录备份通讯录,苹果手机怎么备份通讯录?手机通讯录微信联系人备份教程...
  10. Python分布式爬虫打造搜索引擎