当前位置: 首页 > news >正文

基于SpringBoot+定时任务实现地图上绘制车辆实时运动轨迹图

目录

1. 项目结构

2. Maven依赖配置 (pom.xml)

3. 实现后端服务

 4. 配置文件 (application.properties) 

5. 启动项目

6. 访问页面


实现基于北斗卫星的车辆定位和轨迹图的Maven工程(使用模拟数据),我们将使用以下技术:

  • Spring Boot:作为后端框架,用来提供数据接口。
  • Thymeleaf:作为前端模板引擎,呈现网页。
  • Leaflet.js:一个开源的JavaScript库,用于显示交互式地图。
  • Simulated Data:使用随机生成的模拟GPS数据来模拟北斗卫星车辆位置。
  • WebSocket:用于实现实时数据推送,确保地图位置每秒更新。

1. 项目结构

创建一个Maven项目,基本结构如下:

项目结构图: 

2. Maven依赖配置 (pom.xml)

首先在pom.xml中添加必要的依赖,确保使用Spring Boot、WebSocket和Thymeleaf:

<dependencies><!-- Spring Boot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Thymeleaf for rendering HTML --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- WebSocket for real-time communication --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency><!-- Lombok (Optional, for cleaner code) --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency>
</dependencies>

3. 实现后端服务

package com.example.beidou;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication
@EnableScheduling  // 启用定时任务
public class BeidouApplication {public static void main(String[] args) {SpringApplication.run(BeidouApplication.class, args);}
}
效果图:
Controller:
package com.example.beidou.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;@RestController
public class VehicleController {@Autowiredprivate SimpMessagingTemplate messagingTemplate;private Map<String, Map<String, Double>> vehiclePositions = new HashMap<String, Map<String, Double>>() {{put("Vehicle 1", new HashMap<String, Double>() {{put("latitude", 39.9042);//北京put("longitude", 116.4074);}});put("Vehicle 2", new HashMap<String, Double>() {{put("latitude", 31.2304);//上海put("longitude", 121.4737);}});put("Vehicle 3", new HashMap<String, Double>() {{put("latitude", 22.3964);// 香港put("longitude", 114.1095);}});put("Vehicle 4", new HashMap<String, Double>() {{put("latitude", 30.5728);//成都put("longitude", 104.0668);}});put("Vehicle 5", new HashMap<String, Double>() {{put("latitude", 34.3416);// 西安put("longitude", 108.9398);}});}};private Map<String, Map<String, Double>> vehicleTargets = new HashMap<String, Map<String, Double>>() {{put("Vehicle 1", new HashMap<String, Double>() {{put("latitude", 31.2304);//上海put("longitude", 121.4737);}});put("Vehicle 2", new HashMap<String, Double>() {{put("latitude", 39.9042);//北京put("longitude", 116.4074);}});put("Vehicle 3", new HashMap<String, Double>() {{put("latitude", 34.3416);// 西安put("longitude", 108.9398);}});put("Vehicle 4", new HashMap<String, Double>() {{put("latitude", 22.3964);// 香港put("longitude", 114.1095);}});put("Vehicle 5", new HashMap<String, Double>() {{put("latitude", 30.5728);//成都put("longitude", 104.0668);}});}};// 服务器启动时自动启动模拟@PostConstructpublic void startSimulation() {System.out.println("Starting vehicle simulation...");}// 模拟车辆移动轨迹@Scheduled(fixedRate = 1000)private void sendVehicleUpdates() {Map<String, Map<String, Double>> updatedPositions = new HashMap<>();for (Map.Entry<String, Map<String, Double>> entry : vehiclePositions.entrySet()) {String vehicleId = entry.getKey();Map<String, Double> position = entry.getValue();Map<String, Double> target = vehicleTargets.get(vehicleId);// 按一定速度向目标移动double latDiff = target.get("latitude") - position.get("latitude");double lonDiff = target.get("longitude") - position.get("longitude");// 每次移动经纬度的 1/100double newLatitude = position.get("latitude") + latDiff * 0.02;double newLongitude = position.get("longitude") + lonDiff * 0.02;position.put("latitude", newLatitude);position.put("longitude", newLongitude);updatedPositions.put(vehicleId, new HashMap<>(position));}messagingTemplate.convertAndSend("/topic/vehicleLocation", updatedPositions);}
}

WebSocketConfig : 

package com.example.beidou.config;import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {@Overridepublic void configureMessageBroker(MessageBrokerRegistry config) {config.enableSimpleBroker("/topic");  // 使用 "/topic" 作为消息前缀config.setApplicationDestinationPrefixes("/app");}@Overridepublic void registerStompEndpoints(StompEndpointRegistry registry) {registry.addEndpoint("/vehicle-location").setAllowedOriginPatterns("*").withSockJS();}
}

 4. 配置文件 (application.properties) 

server.port=8080

5. 启动项目

确保你有Java和Maven环境,在项目根目录执行以下命令启动应用:

mvn spring-boot:run

6. 访问页面

在浏览器中访问 http://localhost:8080,你应该可以看到一个地图,显示车辆的实时位置和轨迹更新。

前端页面代码有需要的,请私信我,有偿提供代码,白嫖党勿扰! 

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 第五章 继承、多态、抽象类与接口 课后训练(3)
  • eureka.client.service-url.defaultZone的坑
  • Java是怎么处理死锁的
  • Python|OpenCV-实现识别目标图像中的圆圈(20)
  • 本地部署大模型并使用知识库Windows下Ollama+Docker+MaxKB安装的记录
  • VMware虚拟机经常性卡死,打开运行一段时间后卡死,CPU占比增至100%
  • 【STM32】DAC数字模拟转换
  • 【BurpSuite】Cross-site scripting (XSS 学徒部分:1-9)
  • Go 并发模式:扩展与聚合的高效并行
  • GPT对话知识库——将寄存器中的一位数据读到变量中需要什么步骤?C语言中掩码的作用。
  • SpringBoot使用@Async注解,实现异步任务
  • 硬件工程师笔试面试——无线通讯模块
  • Go并发编程的高级技巧——请求复制与限流
  • 数据结构之二叉树的暴力删除
  • Golang | Leetcode Golang题解之第415题字符串相加
  • C++11: atomic 头文件
  • CSS实用技巧
  • JS变量作用域
  • MySQL用户中的%到底包不包括localhost?
  • Quartz初级教程
  • Spark RDD学习: aggregate函数
  • SpiderData 2019年2月25日 DApp数据排行榜
  • Yeoman_Bower_Grunt
  • 基于Vue2全家桶的移动端AppDEMO实现
  • 浅谈Golang中select的用法
  • 如何进阶一名有竞争力的程序员?
  • 一个JAVA程序员成长之路分享
  • 怎么将电脑中的声音录制成WAV格式
  • ​如何在iOS手机上查看应用日志
  • ‌U盘闪一下就没了?‌如何有效恢复数据
  • # .NET Framework中使用命名管道进行进程间通信
  • #includecmath
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (145)光线追踪距离场柔和阴影
  • (C#)一个最简单的链表类
  • (ISPRS,2021)具有遥感知识图谱的鲁棒深度对齐网络用于零样本和广义零样本遥感图像场景分类
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (PySpark)RDD实验实战——求商品销量排行
  • (笔试题)合法字符串
  • (转)关于多人操作数据的处理策略
  • (轉貼) 2008 Altera 亞洲創新大賽 台灣學生成果傲視全球 [照片花絮] (SOC) (News)
  • .dwp和.webpart的区别
  • .Net - 类的介绍
  • .NET COER+CONSUL微服务项目在CENTOS环境下的部署实践
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .net MySql
  • .NET 分布式技术比较
  • .net实现头像缩放截取功能 -----转载自accp教程网
  • .NET应用架构设计:原则、模式与实践 目录预览
  • /*在DataTable中更新、删除数据*/
  • @ComponentScan比较
  • @Controller和@RestController的区别?
  • @font-face 用字体画图标
  • []sim300 GPRS数据收发程序