高德地图轨迹回放并提示具体信息

先上效果图
在这里插入图片描述
到达某地点后显示提示语:比如:12:56分驶入康庄大道、左转驶入xx大道等

<!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>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=你的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.397428, 39.90923],
            zoom: 26,
             pitch: 55.94919957310569,
            rotation: 0,
            viewMode: '3D', //开启3D视图,默认为关闭
            buildingAnimation: true, //楼块出现是否带动画
            zooms:[2,26],
          
        });

        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 pathArr = [
        { position: new AMap.LngLat(116.478935,39.997761),time:"到达时间1"},
        { position: new AMap.LngLat(116.478939,39.997825),time:"到达时间2"},
        { position: new AMap.LngLat(116.478912,39.998549),time:"到达时间3"},
        { position: new AMap.LngLat(116.478998,39.998555),time:"到达时间4"},
        { position: new AMap.LngLat(116.479282,39.99856),time:"到达时间5"},
        { position: new AMap.LngLat(116.484648,39.999861),time:"到达时间6"},
      ];
      var infoDiv = document.createElement('div');
      infoDiv.id = 'info';
      infoDiv.style.position ='fixed';
      infoDiv.style.left ='50%';
      infoDiv.style.top='0px';
      infoDiv.style.zIndex=100;
      infoDiv.style.background ='#fff';
      infoDiv.style.width='240px';
      infoDiv.style.height='50px';
      document.body.appendChild(infoDiv);

        // 绘制轨迹
        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,      //线宽
        });

        //已知经过的最近时间点
        var lastTimedPointIndex=-1;
        marker.on('moving', function (e) {
            passedPolyline.setPath(e.passedPath);
            map.setCenter(e.target.getPosition(),true)
            // 设置旋转角
            map.setRotation(-e.target.getOrientation());
          var position = e.passedPath[e.passedPath.length-1];
            var timedPoint = pathArr.find(function(p, index){
               var pointPassed = new AMap.LngLat(position.lng,position.lat).distance(p.position)<4;// 判断两点之间的直线距离是否小于4米
              if(pointPassed)lastTimedPointIndex = index;
              return pointPassed;
             })
             //如果找到有时间的点,并且该点是新经过的点,更新infoDiv
             if(timedPoint && lastTimedPointIndex>-1){
               console.log(timedPoint.time)
                // 显示时间信息
                infoDiv.innerHTML = timedPoint.time;
             }
        });

        map.setFitView();

        window.startAnimation = function startAnimation () {
            marker.moveAlong(lineArr, {
                // 每一段的时长
                duration: 500,//可根据实际采集时间间隔设置
                // JSAPI2.0 是否延道路自动设置角度在 moveAlong 里设置
                autoRotation: true,
            });
        };

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

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

        window.stopAnimation = function () {
            marker.stopMove();
        };
    });
</script>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

[廾匸]

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值