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

news/2024/9/22 23:07:24 标签: spring, maven, spring boot

实现基于北斗卫星的车辆定位和轨迹图的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.niftyadmin.cn/n/5670956.html

相关文章

【Linux篇】网络编程基础(笔记)

目录 一、服务器模型 1. C/S 模型 2. P2P模型 二、服务器编程框架 1. I/O处理单元 2. 逻辑单元 3. 网络存储单元 4. 请求队列 三、网络编程基础API 1. socket 地址处理 API &#xff08;1&#xff09;主机字节序和网络字节序 &#xff08;2&#xff09;通用socket地…

开源PHP导航网源码/精美简约网址导航收录网站/QQ技术导航程序

源码简介&#xff1a; 一款给力的开源PHP导航网源码&#xff0c;它不仅外观精美简约&#xff0c;还是个网址导航收录网站/QQ技术导航程序哦&#xff01; 在信息爆炸的时代&#xff0c;找网页就像大海捞针一样难。但是有了像PHP 导航网这样的神器&#xff0c;一切都变得简单了…

Java中stream流及Collectors的常见用法详细汇总!!!

目录 1. Stream流的优势 2. 常用用法 2.1 中间操作 2.1.1filter() 2.1.2 map() 2.1.3 sorted() 2.1.4 distinct() 2.1.5 limit() 2.1.6 skip() 2.2 终端操作 2.2.1 foreach() 2.2.2 collect() 2.2.3 reduce() 2.2.4 count() 2.2.5 anyMatch() 2.3 查找和匹配…

去耦合的一些建议

尽量少用全局变量&#xff0c;以减少状态共享和潜在的副作用。 模块化设计&#xff1a;将代码分成小模块&#xff0c;每个模块独立实现特定功能&#xff0c;减少模块之间的相互依赖。 封装&#xff1a;将数据和操作封装在类中&#xff0c;控制对内部状态的访问&#xff0c;避…

js进阶——作用域闭包

1. 作用域与闭包的基础概念 1.1 作用域 (Scope) 在 JavaScript 中&#xff0c;作用域定义了变量、函数的可访问性。根据变量声明的位置不同&#xff0c;作用域有三种主要类型&#xff1a; 全局作用域&#xff1a;在代码的任何地方都可以访问。函数作用域&#xff1a;只在声明…

Linux(6)--CentOS目录

文章目录 1. 根目录2. cd目录切换命令3. CentOS目录介绍4. pwd命令介绍5. ls命令介绍5.1 ls5.2 ls -a5.3 ls -l 1. 根目录 Windows电脑的根目录是计算机(我的电脑)&#xff0c;然后C盘、D盘。 Linux系统的根目录是/&#xff0c;我们可以使用cd /进入根目录&#xff0c;然后使…

OpenCV特征检测(10)检测图像中直线的函数HoughLinesP()的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 在二值图像中使用概率霍夫变换查找线段。 该函数实现了用于直线检测的概率霍夫变换算法&#xff0c;该算法在文献 181中有所描述。 HoughLines…

javase笔记4----常用类型

常用类型 包装类 简介 java语言是面向对象的语言&#xff0c;但是其中的八大基本数据类型不符合面向对象的特征。 因此java为了弥补这样的缺点&#xff0c;为这八种基本数据类型专门设计了八种符合面向对象特征的的类型&#xff0c;这八种具有面向对象特征的类型&#xff0c;…