栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 整合redis geo 实现查找附近信息功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 整合redis geo 实现查找附近信息功能

工具类:

import lombok.RequiredArgsConstructor;
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.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@RequiredArgsConstructor
public class RedisGeoUtil {
	private final StringRedisTemplate redisTemplate;

	
	public long geoAdd(String key, Double longitude, Double latitude, String member) {
		return redisTemplate.opsForGeo().add(key, new Point(longitude, latitude), member);
	}

	
	public long geoAddList(String key, List> locations) {
		return redisTemplate.opsForGeo().add(key, locations);
	}

	
	public Distance geoDist(String key, String member1, String member2) {
		return redisTemplate.opsForGeo().distance(key, member1, member2);
	}

	
	public List geoHash(String key) {
		return redisTemplate.opsForGeo().hash(key);
	}

	
	public List geoHashList(String key, String[] serviceStations) {
		GeoOperations ops = redisTemplate.opsForGeo();
		return ops.hash(key, serviceStations);
	}

	
	public Point geoPos(String key, String member) {
		return (redisTemplate.opsForGeo().position(key, member)).get(0);
	}

	
	public GeoResults> geoRadius(String key, String member, double distance) {
		RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates()
			.sortAscending();
		return redisTemplate.opsForGeo().radius(key, member, new Distance(distance, Metrics.KILOMETERS), args);
	}


	
	public GeoResults> getPointRadius(String key, Double lng, Double lat, int distance) {
		Circle within = new Circle(new Point(lng, lat), new Distance(distance, Metrics.KILOMETERS));
		RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
			.includeDistance() //包含距离
			.includeCoordinates() //包含经纬度
			.sortAscending()//正序排序
			.limit(50); //条数
		return redisTemplate.opsForGeo().radius(key, within, args);
	}


	
	public Long deleteBatchRedisGeo(String key, String[] name) {
		return redisTemplate.boundZSetOps(key).remove(name);
	}

	
	public Long deleteRedisGeo(String key, String name) {
		return redisTemplate.boundZSetOps(key).remove(name);
	}



public class RedisGeo {

	
	public static String redisKeyCommunityCode = "REDIS_GEO_COMMUNITY_CODE:";

	
	public static String redisGeoCabinetCommunity = "REDIS_GEO_CABINET_COMMUNITY:";
}




@Data
public class QueryNearDTO {

	
	@ApiModelProperty(value = "距离 (公里)", required = true)
	@NotNull
	private Integer distance;
	
	@ApiModelProperty(value = "租户id", hidden = true)
	private String tenantId;

	
	@ApiModelProperty(value = "经度", required = true)
	@NotNull
	private Double lng;

	
	@ApiModelProperty(value = "纬度", required = true)
	@NotNull
	private Double lat;

	
	@ApiModelProperty(value = "获取条数")
	private Integer limit;


}






import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springblade.common.enums.RedisGeo;
import org.springblade.common.utils.RedisGeoUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.wash.dto.QueryNearDTO;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
@Api(value = "附近测试", tags = "附近测试")
public class TestController {

	private final RedisGeoUtil redisGeoUtil;



	@PostMapping("a_geo")
	@ApiOperation(value = "批量添加附近坐标", notes = "添加附近坐标")
	public R addGeo() {
		List> list = new ArrayList<>();
		double[] points = {113.674755, 34.743285,
			113.665556, 34.753013,
			113.731959, 34.74376,
			113.662538, 34.720501,
			113.668287, 34.768197,
			113.668287, 34.768197,
			113.671881, 34.749158,
			113.671881, 34.749158, 113.668287, 34.768197,
			113.668287, 34.768197,
			113.671881, 34.749158,
			113.671881, 34.749158};
		for (int i = 0; i < 20; i += 2) {
			RedisGeoCommands.GeoLocation location = new RedisGeoCommands.GeoLocation("name_" + i / 2, new Point(points[i], points[i + 1]));
			list.add(location);
		}
		long count = redisGeoUtil.geoAddList(RedisGeo.redisKeyCommunityCode + "123456", list);
		return R.data(count);
	}



	@GetMapping("q_geo_add")
	@ApiOperation(value = "单个新增坐标")
	public R q_geo_add() {
		return R.data(redisGeoUtil.geoAdd( RedisGeo.redisKeyCommunityCode + "000000",  new Double(113.543099),  new Double(34.743285),  "666666"));
	}


	@GetMapping("q_geo_if")
	@ApiOperation(value = "查询某点附近距离信息")
	public R getGeo1(String member, double distance) {
		GeoResults> geoResults = redisGeoUtil.geoRadius(RedisGeo.redisKeyCommunityCode + "000000", member, distance);
		return R.data(geoResults);
	}

	@GetMapping("q_geo_local")
	@ApiOperation(value = "查询坐标范围内数据", notes = "查询范围内数据")
	public R>> q_geo_local(QueryNearDTO queryNearDTO) {
		return R.data(redisGeoUtil.getPointRadius(RedisGeo.redisKeyCommunityCode + "000000", queryNearDTO.getLng(), queryNearDTO.getLat(), queryNearDTO.getDistance(),1));
	}



	@GetMapping("q_geo_remove")
	@ApiOperation(value = "删除坐标信息", notes = "删除坐标信息")
	public R q_geo_remove(String member) {
		String[] aa=Arrays.asList("中石化", "666666", "REDIS_GEO_CABINET_COMMUNITY:504756").toArray(new String[3]);
		return R.data(redisGeoUtil.deleteBatchRedisGeo(RedisGeo.redisKeyCommunityCode + "000000", aa));
	}


	
	@GetMapping("q_hash_list")
	@ApiOperation(value = "获取地理位置的 geohash 值")
	public R> testGetServiceStationGeoHash() {
		String[] aa=Arrays.asList("中石化", "中石油", "REDIS_GEO_CABINET_COMMUNITY:504756").toArray(new String[3]);
		List hashList=	redisGeoUtil.geoHashList(RedisGeo.redisKeyCommunityCode + "000000",aa);
		return R.data(hashList);
	}


}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936516.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号