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

news/2024/9/19 8:42:21 标签: spring boot, java, spring

目录

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

java">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:
java">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 {

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

    }};

    // 服务器启动时自动启动模拟
    @PostConstruct
    public 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/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 : 

java">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");  // 使用 "/topic" 作为消息前缀
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public 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,你应该可以看到一个地图,显示车辆的实时位置和轨迹更新。

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


http://www.niftyadmin.cn/n/5665274.html

相关文章

【机器学习】任务五:葡萄酒和鸢尾花数据集分类任务

目录 1.实验基础知识 1.1 集成学习 &#xff08;1&#xff09;随机森林 &#xff08;2&#xff09;梯度提升决策树&#xff08;GBDT&#xff09; &#xff08;3&#xff09;XGBoost &#xff08;4&#xff09;LightGBM 1.2 参数优化 &#xff08;1&#xff09;网格搜索…

Docker Registry API best practice 【Docker Registry API 最佳实践】

文章目录 1. 安装 docker2. 配置 docker4. 配置域名解析5. 部署 registry6. Registry API 管理7. 批量清理镜像8. 其他 &#x1f44b; 这篇文章内容&#xff1a;实现shell 脚本批量清理docker registry的镜像。 &#x1f514;&#xff1a;你可以在这里阅读&#xff1a;https:/…

python学习笔记目录

基于windows下docker安装HDDM-CSDN博客 在python中安装HDDM-CSDN博客&#xff08;这个办法没安装成功&#xff09;

IDEA去除掉虚线,波浪线,和下划线实线的方法

初次安装使用IDEA&#xff0c;总是能看到导入代码后&#xff0c;出现很多的波浪线&#xff0c;下划线和虚线&#xff0c;这是IDEA给我们的一些提示和警告&#xff0c;但是有时候我们并不需要&#xff0c;反而会让人看着很不爽&#xff0c;这里简单记录一下自己的调整方法&#…

图书馆座位预约系统小程序的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;用户管理&#xff0c;图书馆管理&#xff0c;座位信息管理&#xff0c;预约选座管理&#xff0c;签到信息管理&#xff0c;系统管理 微信端账号功能包括&#xff1a;系统首页&#xff0c;论坛&#xf…

Ansys Zemax | 如何使用琼斯矩阵表面

附件下载 联系工作人员获取附件 概览 琼斯矩阵 (Jones Matrix) 表面是一种非常简便的定义偏振元件的方法。这篇文章通过几个示例介绍了如何使用琼斯矩阵。 介绍 光线追迹程序一般只考虑光线的几何属性&#xff08;位置、方向和相位&#xff09;。光线传播到一个表面时的全…

基本概念介绍

基本概念 cluster&#xff1a;cluster是计算&#xff0c;存储和网络资源的集合&#xff0c;Kubernetes利用这些资源运行各种基于容器的应用master&#xff1a;master是cluster的大脑&#xff0c;它的主要职责是调度&#xff0c;即决定将应用放在哪里运行&#xff0c;为了实现高…

Django路由访问及查询数据

1、在应用模块下&#xff0c;创建urls文件&#xff0c;用来存放访问路由 2、在项目总访问url里面注册路由 3、在view文件里&#xff0c;定义方法参数 from django.core import serializers from django.db import connection from django.http import HttpResponse, JsonRespo…