高德地图3D轨迹回放 + 视野跟随功能

  • Post author:
  • Post category:其他




概述


若有帮助到你,麻烦点一波关注,博主会持续推出Echarts,D3,地图,Three.js方面的文章~~~


注意:

高德地图必须使用2.0版本


实现功能点:

  1. 加载高德3D地图
  2. 实现轨迹回放功能
  3. 实现视野跟随功能

    最终实现效果



代码实现步骤

1.加载3D地图

let map = new AMap.Map("container", {
    resizeEnable: true,
    center: [116.479126, 39.998563],
    zoom: 20,
    pitch: 55.94919957310569,
    rotation: -0.7908261543741522,
    viewMode: '3D', //开启3D视图,默认为关闭
    buildingAnimation: true, //楼块出现是否带动画
});

2.添加小车

let marker = new AMap.Marker({
    map: map,
    position: [116.478935, 39.997761],
    icon: "https://a.amap.com/jsapi_demos/static/demo-center-v2/car.png",
    offset: new AMap.Pixel(-13, -26),
});

3.添加小车移动时的监听函数,此处为最重要的代码,在小车开始移动时就一直设置地图中心点和旋转角

marker.on('moving', function (e) {
   // 设置地图中心点
   map.setCenter(e.target.getPosition())
   // 设置旋转角
   map.setRotation(-e.target.getOrientation())
});

4.开启小车运动

window.startAnimation = function startAnimation() {
   marker.moveAlong(lineArr, {
          // 每一段的时长
          duration: 200,
          // JSAPI2.0 是否延道路自动设置角度在 moveAlong 里设置
          autoRotation: true,
         });
};



完整代码 (需要添加一个2.0的key)

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>轨迹回放</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <style>
        html,
        body,
        #container {
            height: 100%;
            width: 100%;
        }

        .input-card .btn {
            margin-right: 1.2rem;
            width: 9rem;
        }

        .input-card .btn:last-child {
            margin-right: 0;
        }
    </style>
</head>

<body>
    <div id="container"></div>
    <div class="input-card">
        <h4>轨迹回放控制</h4>
        <div class="input-item">
            <input type="button" class="btn" value="开始动画" id="start" onclick="startAnimation()" />
            <input type="button" class="btn" value="暂停动画" id="pause" onclick="pauseAnimation()" />
        </div>
        <div class="input-item">
            <input type="button" class="btn" value="继续动画" id="resume" onclick="resumeAnimation()" />
            <input type="button" class="btn" value="停止动画" id="stop" onclick="stopAnimation()" />
        </div>
    </div>
    // 添加一个2.0的key值即可以使用
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=">
    </script>
    <script>
        // JSAPI2.0 使用覆盖物动画必须先加载动画插件
        AMap.plugin('AMap.MoveAnimation', function () {
            var marker, lineArr = [
                [116.478935, 39.997761],
                [116.478939, 39.997825],
                [116.478912, 39.998549],
                [116.478912, 39.998549],
                [116.478998, 39.998555],
                [116.478998, 39.998555],
                [116.479282, 39.99856],
                [116.479658, 39.998528],
                [116.480151, 39.998453],
                [116.480784, 39.998302],
                [116.480784, 39.998302],
                [116.481149, 39.998184],
                [116.481573, 39.997997],
                [116.481863, 39.997846],
                [116.482072, 39.997718],
                [116.482362, 39.997718],
                [116.483633, 39.998935],
                [116.48367, 39.998968],
                [116.484648, 39.999861]
            ];

            var map = new AMap.Map("container", {
                resizeEnable: true,
                center: [116.479126, 39.998563],
                zoom: 20,
                pitch: 55.94919957310569,
                rotation: -0.7908261543741522,
                viewMode: '3D', //开启3D视图,默认为关闭
                buildingAnimation: true, //楼块出现是否带动画
            });

            window.map = map

            marker = new AMap.Marker({
                map: map,
                position: [116.478935, 39.997761],
                icon: "https://a.amap.com/jsapi_demos/static/demo-center-v2/car.png",
                offset: new AMap.Pixel(-13, -26),
            });

            // 绘制轨迹
            var polyline = new AMap.Polyline({
                map: map,
                path: lineArr,
                showDir: true,
                strokeColor: "#28F", //线颜色
                // strokeOpacity: 1,     //线透明度
                strokeWeight: 6, //线宽
                // strokeStyle: "solid"  //线样式
            });

            var passedPolyline = new AMap.Polyline({
                map: map,
                strokeColor: "#AF5", //线颜色
                strokeWeight: 6, //线宽
            });


            marker.on('moving', function (e) {
                passedPolyline.setPath(e.passedPath);
                // 设置地图中心点
                map.setCenter(e.target.getPosition())
                // 设置旋转角
                map.setRotation(-e.target.getOrientation())
            });

            map.setFitView();

            window.startAnimation = function startAnimation() {
                marker.moveAlong(lineArr, {
                    // 每一段的时长
                    duration: 200,
                    // JSAPI2.0 是否延道路自动设置角度在 moveAlong 里设置
                    autoRotation: true,
                });
            };

            window.pauseAnimation = function () {
                marker.pauseMove();
            };

            window.resumeAnimation = function () {
                marker.resumeMove();
            };

            window.stopAnimation = function () {
                marker.stopMove();
            };

        });
    </script>
</body>

</html>



版权声明:本文为qq_39503511原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。