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

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

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

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

目录

1. 项目结构

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

3. 实现后端服务

3.1 BeidouApplication.java

4. 配置文件 (application.properties)

5. 启动项目

6. 访问页面


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. 实现后端服务

3.1 BeidouApplication.java

这是Spring Boot的启动类:

package com.example.beidou;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BeidouApplication {
    public static void main(String[] args) {
        SpringApplication.run(BeidouApplication.class, args);
    }
}

4. 配置文件 (application.properties)

server.port=8080

5. 启动项目

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

mvn spring-boot:run

6. 访问页面

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

效果图:

 Controller:

package com.example.beidou.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@RestController
public class VehicleController {

    private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);

    @Autowired
    private 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);
        }});

    }};

    @GetMapping("/startSimulation")
    public String startSimulation() {
        executorService.scheduleAtFixedRate(this::sendVehicleUpdates, 0, 500, TimeUnit.MILLISECONDS);
        return "Simulation started!";
    }

    @GetMapping("/stopSimulation")
    public String stopSimulation() {
        executorService.shutdownNow();
        return "Simulation stopped!";
    }

    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/100
            double 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 {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/vehicle-location").setAllowedOriginPatterns("*").withSockJS();
    }
}

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


http://www.kler.cn/news/315516.html

相关文章:

  • ARM概念
  • android13 RK3588芯片,录音没有声音
  • AIGC时代算法工程师的面试秘籍(第二十二式2024.9.2-9.15) |【三年面试五年模拟】
  • SVN笔记-SVN安装
  • 【Hot100】LeetCode—295. 数据流的中位数
  • 五、CAN总线
  • C++:动态内存分配(new、delete 相比 malloc、free的优势)与运算符重载
  • 线程池动态设置线程大小踩坑
  • Hadoop的安装和使用
  • 【JavaScript】数据结构之树
  • Qt 学习第十天:小项目:QListWidget的使用
  • 【基于Spring Boot的汽车租赁系统】
  • 【微信小程序】连续拍照功能实现
  • kafka 消息位移提交几种方式:消息重复消息、消息丢失的关键
  • Docker_基础初识
  • 新能源汽车知识点集萃
  • Python办公自动化教程(003):PDF的加密
  • HarmonyOS Next开发----使用XComponent自定义绘制
  • 【乐企-工具篇】有关乐企发票文件生成- OFD和PDF文件生成
  • 四、JVM原理-4.1、JVM介绍
  • vue中 <template> 与 <template lang=“jade“>的对比,哪个性能好
  • 数据结构之希尔排序
  • 轻代码的概念学习笔记
  • http和https的区别及get和post请求的区别
  • Vue3新组件transition(动画过渡)
  • Java API 之集合框架进阶
  • 软件测试面试题(5)——二面(游戏测试)
  • 【PLW003】设备器材云端管理平台v1.0(SpringBoot+Mybatis+NodeJS+MySQL前后端分离)
  • LeetCode题练习与总结:回文链表--234
  • [JavaEE]———进程、进程的数据结构、进程的调度