小程序实现定位城市切换且城市根据首字母A-Z排序后端数据实现逻辑

news/2024/5/21 3:49:14/文章来源:https://blog.csdn.net/qq_40083897/article/details/136363980

场景:

话不多说后端提供数据实现步骤:

1.controller层

@Api(tags = {"[地区]-城市相关接口"})
@RestController
@RequestMapping("region")
@Slf4j
public class RegionController extends BaseController {@Resourceprivate RegionService regionService;@ApiOperation("城市列表[A-Z]")@ApiImplicitParams({@ApiImplicitParam(name = "condition", value = "城市名称", required = false, paramType = "query")})@GetMapping("cityList")public JsonResult<List<AppletCityListVO>> cityList(@RequestParam(value = "condition", required = false) String condition) {return JsonResult.ok(this.regionService.smallProgramCityDisplayList(condition));}@ApiOperation("城市列表")@ApiImplicitParams({@ApiImplicitParam(name = "condition", value = "城市名称", required = false, paramType = "query")})@GetMapping("queryCityList")public JsonResult<List<AppletCityListVO.CityItem>> queryCityList(@RequestParam(value = "condition", required = false) String condition) {return JsonResult.ok(this.regionService.queryCityList(condition));}@Resourceprivate TencentMapUtil tencentMapUtil;@ApiOperation("根据经纬度查询地址")@GetMapping("tencent/map/reGeoCoder")public JsonResult<JSONObject> reGeoCoder(String lat, String lng) {AssertUtil.notBlank(lat, "纬度不能为空");AssertUtil.notBlank(lng, "经度不能为空");JSONObject jsonObject = tencentMapUtil.reGeoCoder(lat, lng);if (EntityConstant.INVALID != jsonObject.getInteger("status")) {log.error("定位授权失败 , errorMsg = {}", jsonObject.getString("message"));throw SimpleException.getInstance("定位授权失败");}JSONObject adInfo = jsonObject.getJSONObject("result").getJSONObject("ad_info");String city_code = adInfo.getString("city_code");String nation_code = adInfo.getString("nation_code");jsonObject.getJSONObject("result").getJSONObject("ad_info").put("cityCode", city_code.replaceAll(nation_code, ""));return JsonResult.ok(jsonObject);}
}

2.service层

@Service
@Slf4j
@Primary
public class RegionServiceImpl extends BaseApplicationService implements RegionService {@Resourceprivate RegionMapper regionMapper;private static final char[] alphabet = new char[26];static {// 使用循环填充数组元素for (int i = 0; i < alphabet.length; i++) {alphabet[i] = (char) ('A' + i);}}@Overridepublic List<AppletCityListVO> smallProgramCityDisplayList(String condition) {List<Region> regionList = this.regionMapper.selectByExample(new ExampleLambda<>(Region.class).andEqualTo(Region::getStatus, EntityConstant.NORMAL).andEqualTo(Region::getLevel, 2).andLike(Objects.nonNull(condition), Region::getName, condition).end());Map<String, List<Region>> cityMap = regionList.stream().collect(Collectors.groupingBy(Region::getInitial));List<AppletCityListVO> cityListVOList = new ArrayList<>(this.alphabet.length);for (char c : this.alphabet) {List<AppletCityListVO.CityItem> tempList = cityMap.containsKey(String.valueOf(c)) ? cityMap.get(String.valueOf(c)).stream().map(item -> {AppletCityListVO.CityItem cityItem = new AppletCityListVO.CityItem();cityItem.setCode(item.getCode());cityItem.setName(item.getName());return cityItem;}).collect(Collectors.toList()) : new ArrayList<>(0);cityListVOList.add(AppletCityListVO.builder().initial(String.valueOf(c)).itemList(tempList).build());}return cityListVOList;}@Overridepublic List<AppletCityListVO.CityItem> queryCityList(String condition) {List<Region> regionList = this.regionMapper.selectByExample(new ExampleLambda<>(Region.class).andEqualTo(Region::getStatus, EntityConstant.NORMAL).andEqualTo(Region::getLevel, 2).andLike(Objects.nonNull(condition), Region::getName, condition).end());return regionList.stream().map(item -> {AppletCityListVO.CityItem cityItem = new AppletCityListVO.CityItem();cityItem.setCode(item.getCode());cityItem.setName(item.getName());return cityItem;}).collect(Collectors.toList());}}@Builder
@Data
public class AppletCityListVO  implements Serializable {@ApiModelProperty(value = "首字母")private String initial;@ApiModelProperty(value = "城市列表")private List<CityItem> itemList;@Datapublic static class CityItem {@ApiModelProperty(value = "code")private String code;@ApiModelProperty(value = "名称")private String name;public CityItem(String code, String name) {this.code = code;this.name = name;}public CityItem() {}}}

3.mapper层

public interface RegionMapper extends CrudMapper<Region, Long> {}


4.实体类

@Table(name = "`region`")
@Data
public class Region extends BaseEntity<Long> implements Serializable {private static final long serialVersionUID = 1L;/*** Id*/@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;/*** 1*/@Column(name = "level")private Integer level;/*** code*/@Column(name = "code")private String code;/*** 父级code*/@Column(name = "parent_code")private String parentCode;/*** 名称*/@Column(name = "name")private String name;/*** 名称拼音*/@Column(name = "pinyin")private String pinyin;/*** 首字母*/@Column(name = "initial")private String initial;/*** 状态*/@Column(name = "status")private Integer status;/*** 创建时间*/@Column(name = "gmt_created")@JsonFormat(locale = "zh", pattern = "yyyy-MM-dd HH:mm:ss")private Date gmtCreated;/*** ModifyUser*/@Column(name = "modify_user")private String modifyUser;/*** CreateUser*/@Column(name = "create_user")private String createUser;/*** 更新时间*/@Column(name = "gmt_modified")@JsonFormat(locale = "zh", pattern = "yyyy-MM-dd HH:mm:ss")private Date gmtModified;/*** 是否删除*/@Column(name = "deleted")private Integer deleted;
}


5.腾讯地图处理工具类

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;import java.util.HashMap;
import java.util.Objects;@Slf4j
@Component
public class TencentMapUtil {private String qqAk = "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-xxxxx";private String geoCoder = "https://apis.map.qq.com/ws/geocoder/v1/";private String distance = "https://apis.map.qq.com/ws/distance/v1/";private String translate = "https://apis.map.qq.com/ws/coord/v1/translate";private String reGeoCoder = "https://apis.map.qq.com/ws/geocoder/v1/?location=";/*** 通过地址查询经纬度** @param addr 地址* @return*/public String geoCoder(String addr) {JSONObject jsonObject = geoCoderRequest(addr);if (null == jsonObject) {return null;}JSONObject location = JSONUtil.parseObj(JSONUtil.toJsonStr(jsonObject.get("location")));if (Objects.nonNull(location) && Objects.nonNull(location.get("lat")) && Objects.nonNull(location.get("lng"))) {return location.get("lat").toString() + "," + location.get("lng").toString();}return null;}public JSONObject geoCoderRequest(String addr) {try {if (StringUtils.isNotEmpty(addr)) {String url = geoCoder +"?address=" + addr +"&key=" + qqAk;// 腾讯地图akString resultStr = HttpUtil.get(url);JSONObject tencentGeo = JSONUtil.parseObj(resultStr);log.info("地址转坐标响应结果:" + JSONUtil.toJsonStr(tencentGeo));if (Objects.nonNull(tencentGeo) && Objects.equals(0, tencentGeo.get("status"))) {return JSONUtil.parseObj(JSONUtil.toJsonStr(tencentGeo.get("result")));}}} catch (Exception e) {log.error("地址转坐标失败,地址:" + addr + ", e: " + e);}return null;}/*** 根据经纬度查询地址** @param lat 纬度* @param lng 经度* @return*/public JSONObject reGeoCoder(String lat, String lng) {StringBuilder url = new StringBuilder(reGeoCoder).append(lat).append(",").append(lng).append("&key=").append(qqAk);String resultStr = HttpUtil.get(url.toString());return JSONUtil.parseObj(resultStr);}/*** 将微信小程序经纬度转换成腾讯地图经纬度** @param lat 纬度* @param lng 经度* @return*/public JSONObject translate(String lat, String lng) {StringBuilder url = new StringBuilder(translate).append("?key=").append(qqAk)// 腾讯地图ak.append("&type=1")// 1.GPS坐标 2.sogou经纬度 3.baidu经纬度 4.mapbar经纬度 5.[默认]腾讯、google、高德坐标 6.sogou墨卡托.append("&&locations=").append(lat).append(",").append(lng);String resultStr = HttpUtil.get(url.toString());return JSONUtil.parseObj(resultStr);}}
6.region.sql文件 

 链接: https://pan.baidu.com/s/1m1WEgQPGXbUzgZ4qusvegQ 提取码: ztr6 复制这段内容后打开百度网盘手机App,操作更方便哦

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_988141.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

9 款顶级 iPhone 系统修复软件,可修复各种 iPhone 软件问题

iOS的封闭性和纯粹性仍然无法让iPhone免受潜在风险的影响。iPhone 存在常见问题&#xff0c;包括iPhone/iPad 卡住 Apple 徽标、iOS 更新无法充电问题、iPhone 耳机问题等等。 通常&#xff0c;在这种情况下&#xff0c;您的 iPhone 数据可能无法访问&#xff0c;甚至面临很大…

爬虫IP代理池的搭建与使用指南

目录 一、爬虫IP代理池的重要性 二、搭建IP代理池 选择合适的代理IP源 搭建代理IP池服务器 实现代理IP的获取和更新 三、使用IP代理池 配置爬虫程序 调用代理IP池API 实现代理IP的自动切换 四、案例与代码 五、总结 随着互联网的迅猛发展&#xff0c;网络爬虫作为一…

用于制作耳机壳的倒模专用UV树脂有什么特点?

制作耳机壳的UV树脂耳机壳UV胶具有以下特点&#xff1a; 快速固化&#xff1a;UV树脂可以在紫外线的照射下迅速固化&#xff0c;大大缩短了制作时间。高硬度与高耐磨性&#xff1a;UV树脂具有较高的硬度和耐磨性&#xff0c;能够提供良好的保护效果。透明度高&#xff1a;UV树…

selenium爬虫

方法选择和安装包 在动态网页并且登陆过程中不需要进行过于复杂的密码验证的时候使用selenium会非常的方便 安装准备过程也相对简单&#xff1a; 下载对应版本的chromedriver并且通过如下代码找到路径下载到python所在的目录&#xff1a; import sysprint(sys.executable) …

普中51单片机学习(LCD1602)

LCD1602 1602液晶也叫1602字符型液晶&#xff0c;它是一种专门用来显示字母、数字、符号的点阵型液晶模块。它是由若干个5x7或者5x10的点阵字符位组成&#xff0c;每个点阵字符位都可以用显示一个字符&#xff0c;每位之间有一个点距的间隔&#xff0c;每行之间也有间隔&#…

Home Assistant:基于Python的智能家居开源系统详解

Home Assistant&#xff1a;基于Python的智能家居开源系统详解 在数字化和智能化的时代&#xff0c;智能家居系统成为了现代家庭的新宠。它们能够让我们更加方便地控制家中的各种设备&#xff0c;实现自动化和个性化的居住体验。其中&#xff0c;Home Assistant作为一款基于Pyt…

Linux Shell脚本练习(一)

一、 Linux下执行Shell脚本的方式&#xff1a; 1、用shell程序执行脚本&#xff1a; a、根据你的shell脚本的类型&#xff0c;选择shell程序&#xff0c;常用的有sh&#xff0c;bash&#xff0c;tcsh等 b、程序的第一行#!/bin/bash里面指明了shell类型的&#xff0c;比如#!/…

【行业科普】常见的边缘计算产品有哪些?主要应用于哪些场景?

之前的几期科普文给大家介绍了什么是边缘计算&#xff0c;以及它的优势、应用场景等内容。有兴趣的可以戳链接再了解一下。&#xff08;【行业科普】边缘计算有多强&#xff1f;一起了解它的优势及其5大典型应用&#xff01;&#xff09;今天我们再来了解一下常见的边缘计算产品…

C# 获取类型 Type.GetType()

背景 C#是强类型语言&#xff0c;任何对象都有Type&#xff0c;有时候需要使用Type来进行反射、序列化、筛选等&#xff0c;获取Type有Type.GetType, typeof()&#xff0c;object.GetType() 等方法&#xff0c;本文重点介绍Type.GetType()。 系统类型/本程序集内的类型 对于系…

2023年全国职业院校技能大赛中职组大数据应用与服务赛项题库参考答案陆续更新中,敬请期待…

2023年全国职业院校技能大赛中职组大数据应用与服务赛项题库参考答案陆续更新中&#xff0c;敬请期待… 武汉唯众智创科技有限公司 2024 年 2 月 联系人&#xff1a;辜渝傧13037102709 题号&#xff1a;试题01 模块三&#xff1a;业务分析与可视化 &#xff08;一&#xff0…

基于springboot+vue的党员教育和管理系统

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…

SOLIDWORKS参数化设计之工程图更新 慧德敏学

SOLIDWORKS参数化设计不仅仅包括三维模型的参数化设计&#xff0c;还包括工程图的自动更新&#xff0c;由于自动出图仍然存在一定的局限性&#xff0c;不能完美的实现视图的布局及尺寸的标注&#xff0c;因此&#xff0c;现阶段采用的最多的仍然是图纸的更新&#xff0c;也就是…

创作纪念日【第1024天】的感触与期待

我的创作纪念日 1.机缘2.日常3.成就4.收获5.憧憬 1.机缘 2021年的04月份进入新行业了&#xff0c;公司不让用自己的电脑进行办公&#xff0c;也不能带自己的设备&#xff0c;之前记录的笔记查看不太方便&#xff0c;所以想着弄个博客&#xff0c;也尝试过云笔记&#xff0c;最…

Java根据excel模版导出Excel(easyexcel、poi)——含项目测试例子拿来即用

Java根据excel模版导出Excel&#xff08;easyexcel、poi&#xff09;——含项目测试例子拿来即用 1. 前言1.1 关于Excel的一般导出2.2 关于easyexcel的根据模版导出 2. 先看效果2.1 模版2.2 效果 3. 代码实现&#xff08;核心代码&#xff09;3.1 项目代码结构3.2 静态填充例子…

Springboot项目中定时任务的四种实现方式

文章目录 1. 使用Scheduled注解1.1 时间间隔执行1.2 固定时间点执行 2. 使用EnableScheduling注解启用定时任务3. 实现SchedulingConfigurer接口4. 使用Quartz框架4.1 配置QuartzScheduler4.2 定义Job类和Trigger类 5. 总结 在开发现代应用时&#xff0c;定时任务是一个非常常见…

Peter算法小课堂—动态规划

Peter来啦&#xff0c;好久没有更新了呢 今天&#xff0c;我们来讨论讨论提高组的动态规划。 动态规划 动态规划有好多经典的题&#xff0c;有什么背包问题、正整数拆分、杨辉三角……但是&#xff0c;如果考到陌生的题&#xff0c;怎么办呢&#xff1f;比如说2000年提高组的…

压缩视频大小的软件有哪些?5款软件推荐

压缩视频大小的软件有哪些&#xff1f;随着高清摄像设备的普及和网络速度的不断提升&#xff0c;视频文件变得越来越庞大&#xff0c;动辄数百兆甚至数GB的大小常常让用户在分享和存储时感到头疼。幸运的是&#xff0c;市面上有许多优秀的视频压缩软件可以帮助我们轻松应对这一…

国产服务器操作系统

为何记录 最近的开发工作经常接触到国产服务器操作系统的业务&#xff0c;经常被x86、arm、龙芯、鲲鹏、欧拉...搞得一脸懵逼&#xff0c;遂记之&#xff01; 操作系统 这里按照应用场景分&#xff1a; 桌面操作系统&#xff1a;主要用于pc&#xff0c;如Windows、macOS、Li…

SpringMVC01、回顾MVC

1、回顾MVC 1.1、什么是MVC MVC是模型(Model)、视图(View)、控制器(Controller)的简写&#xff0c;是一种软件设计规范。是将业务逻辑、数据、显示分离的方法来组织代码。MVC主要作用是降低了视图与业务逻辑间的双向偶合。MVC不是一种设计模式&#xff0c;MVC是一种架构模式。…

ETL数据仓库的使用方式

一、ETL的过程 在 ETL 过程中&#xff0c;数据从源系统中抽取&#xff08;Extract&#xff09;&#xff0c;经过各种转换&#xff08;Transform&#xff09;操作&#xff0c;最后加载&#xff08;Load&#xff09;到目标数据仓库中。以下是 ETL 数仓流程的基本步骤&#xff1a…