当前位置:网站首页>百度地图——地图找房功能
百度地图——地图找房功能
2022-08-09 14:50:00 【代码搬运工!!!】

代码地址:https://github.com/huiyan-fe/BMapGLLib



HouseSearchService层
package cn.itcast.baidumap.service;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.itcast.baidumap.pojo.BusinessCircle;
import cn.itcast.baidumap.pojo.Community;
import cn.itcast.baidumap.pojo.District;
import cn.itcast.baidumap.pojo.House;
import cn.itcast.baidumap.vo.HouseResultVo;
import com.mongodb.internal.operation.AggregateOperation;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Box;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Service
public class HouseSearchService {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 地图找房搜索服务
*
* @param maxLongitude 最大经度
* @param minLongitude 最小经度
* @param maxLatitude 最大纬度
* @param minLatitude 最小纬度
* @param zoom 地图缩放比例值
* @return
*/
public List<HouseResultVo> search(Double maxLongitude,
Double minLongitude,
Double maxLatitude,
Double minLatitude,
Double zoom) {
//收集聚合查询条件
List<AggregationOperation> operationList = new ArrayList<>();
//在可视范围内搜索
Box box = new Box(new double[]{maxLongitude, maxLatitude}, new double[]{minLongitude, minLatitude});
MatchOperation matchOperation = Aggregation.match(Criteria.where("location").within(box));
operationList.add(matchOperation);
int type;
GroupOperation groupOperation;
//根据地图的缩放比例进行分组
if (zoom < 13.5) { //2公里以上
//按照行政区分组
groupOperation = Aggregation.group("districtCode");
type = 1;
} else if (zoom < 15.5) { //200米以上
//按照商圈分组
groupOperation = Aggregation.group("businessCircleCode");
type = 2;
} else { //200以下
//按照小区分组
groupOperation = Aggregation.group("communityId");
type = 3;
}
groupOperation = groupOperation.count().as("total")
.avg("price").as("price");
operationList.add(groupOperation);
//生成最终的聚合条件
Aggregation aggregation = Aggregation.newAggregation(operationList);
//执行查询
AggregationResults<HouseResultVo> aggregationResults = this.mongoTemplate.aggregate(aggregation, House.class, HouseResultVo.class);
List<HouseResultVo> houseResultVoList = aggregationResults.getMappedResults();
if (CollUtil.isEmpty(houseResultVoList)) {
return Collections.emptyList();
}
//填充数据
switch (type) {
case 1: {
//查询行政区数据
for (HouseResultVo houseResultVo : houseResultVoList) {
District district = this.queryDistrictByCode(Convert.toInt(houseResultVo.getCode()));
houseResultVo.setName(district.getName());
houseResultVo.setLongitude(district.getLocation().getX());
houseResultVo.setLatitude(district.getLocation().getY());
//价格保留2位小数
houseResultVo.setPrice(NumberUtil.roundStr(houseResultVo.getPrice(), 2));
}
break;
}
case 2: {
//查询商圈数据
for (HouseResultVo houseResultVo : houseResultVoList) {
BusinessCircle businessCircle = this.queryBusinessCircleByCode(Convert.toInt(houseResultVo.getCode()));
houseResultVo.setName(businessCircle.getName());
houseResultVo.setLongitude(businessCircle.getLocation().getX());
houseResultVo.setLatitude(businessCircle.getLocation().getY());
//价格保留2位小数
houseResultVo.setPrice(NumberUtil.roundStr(houseResultVo.getPrice(), 2));
}
break;
}
case 3: {
//查询小区数据
for (HouseResultVo houseResultVo : houseResultVoList) {
Community community = this.queryCommunityById(new ObjectId(houseResultVo.getCode()));
houseResultVo.setName(community.getName());
houseResultVo.setLongitude(community.getLocation().getX());
houseResultVo.setLatitude(community.getLocation().getY());
//价格保留2位小数
houseResultVo.setPrice(NumberUtil.roundStr(houseResultVo.getPrice(), 2));
}
break;
}
default: {
return Collections.emptyList();
}
}
return houseResultVoList;
}
/**
* 根据code查询行政区数据
*
* @param code
* @return
*/
private District queryDistrictByCode(Integer code) {
Query query = Query.query(Criteria.where("code").is(code));
return this.mongoTemplate.findOne(query, District.class);
}
/**
* 根据code查询商圈数据
*
* @param code
* @return
*/
private BusinessCircle queryBusinessCircleByCode(Integer code) {
Query query = Query.query(Criteria.where("code").is(code));
return this.mongoTemplate.findOne(query, BusinessCircle.class);
}
/**
* 根据code查询小区数据
*
* @return
*/
private Community queryCommunityById(ObjectId id) {
return this.mongoTemplate.findById(id, Community.class);
}
}
HouseSearchController层
package cn.itcast.baidumap.controller;
import cn.itcast.baidumap.service.HouseSearchService;
import cn.itcast.baidumap.vo.HouseResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RequestMapping("house/search")
@RestController
public class HouseSearchController {
@Autowired
private HouseSearchService houseSearchService;
/**
* 地图找房搜索服务
*
* @param maxLongitude 最大经度
* @param minLongitude 最小经度
* @param maxLatitude 最大纬度
* @param minLatitude 最小纬度
* @param zoom 地图缩放比例值
* @return
*/
@GetMapping
public List<HouseResultVo> search(@RequestParam("maxLongitude") Double maxLongitude,
@RequestParam("minLongitude") Double minLongitude,
@RequestParam("maxLatitude") Double maxLatitude,
@RequestParam("minLatitude") Double minLatitude,
@RequestParam("zoom") Double zoom) {
return this.houseSearchService.search(maxLongitude, minLongitude, maxLatitude, minLatitude, zoom);
}
}
最终效果:

边栏推荐
- golang中的select原理解析
- 【DevOps】jekinsBuild step ‘Execute shell‘ marked build as failure
- C语言程序设计笔记(浙大翁恺版) 第一周:程序设计与C语言
- 和月薪5W的测试聊过后,才知道自己一直在打杂...
- WebShell简介
- C语言程序设计笔记(浙大翁恺版) 第七章:函数
- Similar image detection method
- [Serilog] Simple .NET logging with fully structured events
- [Microservice] Detailed explanation of nacos registration center and configuration center
- Analysis of select principle in golang
猜你喜欢
随机推荐
Docker安装MySQL详细步骤
leetcode 剑指 Offer 17. 打印从1到最大的n位数
VMWare不使用简易安装,手动安装ISO操作手册
基于FPGA的FIR滤波器的实现(2)—采用kaiserord & fir2 & firpm函数设计
从软件哲学角度谈 Amazon SageMaker(第一讲)
Analysis of select principle in golang
C语言程序设计笔记(浙大翁恺版) 第七章:函数
spacedesk-notebook, tablet, extended screen-solve the problem that the tablet font is too small
【磁场建模项目2020-02-Lilin】采集板硬件规范
Mongodb增加权限管理
正则表达式实战:最新豆瓣top250爬虫超详细教程
由于谷歌版本自动更新,导致selenium项目报错,如何关闭谷歌浏览器的自动更新?
leetcode_jz
极限挑战,如何做到分钟级搭建环境?
百度开源e-chart初探
MySQL 原理与优化:Limit 查询优化
SMI 与 Gateway API 的 GAMMA 倡议意味着什么?
各种程序员线学习学习教程收集
概率论基础知识整理 | 随机变量
【DevOps】jekins部署(一)









