Baidu map JavaScript API GL realizes vehicle track function

Baidu Maps JavaScript API GL is a map display method based on WebGL technology provided by Baidu Maps. It can present 3D map scenes in a more efficient way in modern browsers, and supports custom layers and animation effects, which can be applied to various map application scenarios.

Baidu Maps JavaScript API GL provides rich functional interfaces, and the following functions can be realized through the API:

  1. 3D map display: use WebGL technology to display high-quality 3D map scenes in the browser;

  2. Dynamic rendering: support dynamic map display effects, such as satellite cloud images, temperature distribution, etc.;

  3. Custom layers: developers can add custom layers through the API, and freely draw and update content on the map;

  4. Path planning: Provides path planning function, supports walking, cycling, driving and other travel modes;

  5. Geocoding: Support converting addresses to latitude and longitude coordinates, or converting coordinates to addresses;

  6. Geographical information query: Provide geographic information query services such as POI query and surrounding query.

Using Baidu Maps JavaScript API GL, developers can develop more efficient and feature-rich map applications and achieve better map display effects.

Baidu map JavaScript API GL can realize the vehicle track function. The following are the specific implementation steps:

  1. Obtain vehicle track data: The historical track data of vehicles can be obtained from the background, stored in an array, and arranged in chronological order.

  2. Create and display a map: Use Baidu Maps JavaScript API GL to create a map and display the map on the page. You can set the map's center point, zoom level and other properties.

  3. Create and add vehicle annotations: Use Baidu Maps JavaScript API GL to create vehicle annotations and add the annotations to the map. You can set the icon, position, label and other properties of the label.

  4. Real-time display of vehicle position: Read the latitude and longitude information at the current moment from the vehicle track data, and move the vehicle label to the current position.

  5. Draw vehicle trajectory: Use Baidu Maps JavaScript API GL to create a polyline (or polygon) object, and draw the trajectory of the vehicle based on the vehicle trajectory data.

  6. Control vehicle movement: Use JavaScript timers to control the movement of vehicle annotations, and update the display of vehicle trajectories in real time.

Through the above steps, the vehicle trajectory function can be realized. It should be noted that in order to improve the map display effect and performance, you can use related functions provided by Baidu Maps JavaScript API GL, such as custom layers, grayscale maps, dynamic effects, etc.
The following is a simple sample code to show how to use Baidu Maps JavaScript API GL to realize the vehicle track function:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>车辆轨迹</title>
    <style type="text/css">
        #container {
      
      
            width: 100%;
            height: 600px;
        }
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api/map-gl/v1.1.0/mapbox-gl.js"></script>
    <link rel="stylesheet" href="http://api.map.baidu.com/api/map-gl/v1.1.0/mapbox-gl.css"/>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
    var map = new mapboxgl.Map({
      
      
        container: 'container',
        style: 'http://mapbox.cn/api/gl-styles/1.0.0/style-lights-v8.json',
        center: [114.305211, 30.592935],
        zoom: 12
    });

    // 车辆轨迹数据
    var trackData = [
        {
      
      "lng":114.315211,"lat":30.592935,"time":"2022-03-01 10:10:00"},
        {
      
      "lng":114.317211,"lat":30.594935,"time":"2022-03-01 10:12:00"},
        {
      
      "lng":114.319211,"lat":30.596935,"time":"2022-03-01 10:14:00"},
        {
      
      "lng":114.321211,"lat":30.598935,"time":"2022-03-01 10:16:00"},
        {
      
      "lng":114.323211,"lat":30.600935,"time":"2022-03-01 10:18:00"}
    ];

    // 创建车辆标注
    var carMarker = new mapboxgl.Marker({
      
      color: 'red'})
        .setLngLat([trackData[0].lng, trackData[0].lat])
        .addTo(map);

    // 绘制车辆轨迹
    var trackLine = {
      
      
        "type": "FeatureCollection",
        "features": [{
      
      
            "type": "Feature",
            "geometry": {
      
      
                "type": "LineString",
                "coordinates": []
            },
            "properties": {
      
      }
        }]
    };
    for (var i = 0; i < trackData.length; i++) {
      
      
        trackLine.features[0].geometry.coordinates.push([trackData[i].lng, trackData[i].lat]);
    }
    map.on('load', function () {
      
      
        // 添加轨迹图层
        map.addSource('track', {
      
      
            "type": "geojson",
            "data": trackLine
        });
        map.addLayer({
      
      
            "id": "track",
            "type": "line",
            "source": "track",
            "layout": {
      
      
                "line-join": "round",
                "line-cap": "round"
            },
            "paint": {
      
      
                "line-color": "#0000ff",
                "line-width": 4
            }
        });
    });

    // 控制车辆运动
    var index = 0;
    var interval = setInterval(function () {
      
      
        index++;
        if (index < trackData.length) {
      
      
            // 更新车辆位置
            carMarker.setLngLat([trackData[index].lng, trackData[index].lat]);
            // 设置地图中心点
            map.setCenter([trackData[index].lng, trackData[index].lat]);
        } else {
      
      
            clearInterval(interval);
        }
    }, 1000);
</script>
</body>
</html>

Note that the above code uses the latest version of Baidu Maps JavaScript API GL, and it needs to register a developer account and apply for an API Key from the Baidu Maps Open Platform to use it. At the same time, vehicle trajectory data also needs to be implemented in the form of specific page parameters.

Guess you like

Origin blog.csdn.net/qq_27487739/article/details/131156055