美文网首页
高德地图开放平台 模拟导航 路径回放

高德地图开放平台 模拟导航 路径回放

作者: 东方三篇 | 来源:发表于2020-03-30 16:27 被阅读0次

模拟导航 路径回放

<!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 id="move_icon" style="background-image: linear-gradient(to right,#ccc,#00f, #ccc); width: 120px; height: 4px;"></div>
    <div id="move_icon2" style="background-image: linear-gradient(to right,#ccc,#0f0, #ccc); width: 120px; height: 4px;"></div>
</div>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
<script>
    var moveIcon = document.getElementById('move_icon')
    var moveIcon2 = document.getElementById('move_icon2')
    var opacity = 1
    var step = 0.1
    setInterval(()=>{
      if (opacity >= 1) {
        step = -step
      } else if (opacity <= 0) {
        step = -step
      }
      opacity += step
      moveIcon.style.opacity = opacity
      moveIcon2.style.opacity = opacity
    }, 40)
    var marker,marker2, 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.397428, 39.90923],
        zoom: 17
    });

    marker = new AMap.Marker({
        map: map,
        position: [116.478935,39.997761],
        // icon: "https://webapi.amap.com/images/car.png",
        content: moveIcon,
        offset: new AMap.Pixel(0, 0),
        autoRotation: true,
        angle:-90,
        zIndex: 100
        // moveend: function(){console.log('moveend')}
    });
    marker2 = new AMap.Marker({
        map: map,
        position: [116.484648,39.999861],
        // icon: "https://webapi.amap.com/images/car.png",
        content: moveIcon2,
        offset: new AMap.Pixel(0, 0),
        autoRotation: true,
        angle:-90,
        zIndex: 110
        // moveend: function(){console.log('moveend')}
    });

    // 绘制轨迹
    var polyline = new AMap.BezierCurve({
        map: map,
        path: lineArr,
        showDir: false, // 是否延路径显示白色方向箭头
        strokeColor: "rgba(0, 0, 0, 0.4)",  //线颜色
        isOutline: true,
        outlineColor: 'rgba(0, 0, 0, 0.4)',
        // lineJoin: 'round', // 折线拐点的绘制样式
        // lineCap: 'round', // 折线两端线帽的绘制样式
        borderWeight: 10,
        // strokeOpacity: 1,     //线透明度
        strokeWeight: 2,      //线宽
        // strokeStyle: "solid"  //线样式
    });

    var passedPolyline = new AMap.Polyline({
        map: map,
        // path: lineArr,
        strokeColor: "#F00",  //线颜色
        // strokeOpacity: 1,     //线透明度
        strokeWeight: 2,      //线宽
        // strokeStyle: "solid"  //线样式
    });


    // marker.on('moving', function (e) {
    //     passedPolyline.setPath(e.passedPath);
    // });

    map.setFitView();

    function startAnimation () {
        // marker.setAnimation('AMAP_ANIMATION_BOUNCE');
        marker.moveAlong(lineArr, 600)
        marker2.moveAlong(lineArr.reverse(), 600)
        setInterval(()=>{
          marker.resumeMove();
          marker2.resumeMove();
        }, 100)
        
    }

    function pauseAnimation () {
        marker.pauseMove();
        // marker.hide()
    }

    function resumeAnimation () {
        marker.resumeMove();
        // maker.show()
    }

    function stopAnimation () {
        marker.stopMove();
    }
    // setTimeout(() => {
    //   startAnimation()
    // }, 1000);
</script>
</body>
</html>

相关文章

网友评论

      本文标题:高德地图开放平台 模拟导航 路径回放

      本文链接:https://www.haomeiwen.com/subject/nlvvuhtx.html