高德地图添加轨迹回显

修改后的:

<!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 src="js/jquery-3.1.1.min.js"></script>
<!--key一定要引入高德地图你的key-->
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=c1c0d2288a11c68caff7c6bbc1c83672"></script>
<script>

    /**
     * 获得本页面地址栏参数
     * 用法:getUrlAddressParameter("name")
     * @param parameter
     * @returns {*}
     * @author tongyao
     */
    function getUrlAddressParameter(parameter){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == parameter){return pair[1];}
        }
        return null;
    }

    /*声明x,y坐标*/
    var xy = [];

    /*声明marker和lineArr,用来启用关闭播放轨迹动画*/
    var marker, lineArr = [];

    /*声明巡检过程中的场所提交点位*/
    var SBINFO = [];


    $(function (){
        //异步请求获取x,y坐标结果
        $.ajax({
            type:"post",
            url: "/uacp_yunguan/datainterface/getdata/list/-1/GJRWATIDCXGJXX-2021518173734",
            contentType : 'application/json',

            //关闭异步
            async:false,
            data:{
                "RWATID":getUrlAddressParameter("RWATID")
            },
            success: function(data) {
                //循环渐进,遍历获取x,y坐标
                for(var i = 0;i<data.data.length;i++){
                    //声明空子数组
                    var obj = [];
                    //逐步获取x和y填入空子数组
                    obj[0] = data.data[i].X
                    obj[1] = data.data[i].Y
                    xy.push(obj);

                    //记录点位提交的数据,思路:判断获取出来的值是否有设备id,有的话在创建新的空子数组
                    if(data.data[i].SBID != ""){
                        //声明空子数组
                        var obj = [];
                        //逐步获取x和y填入空子数组
                        obj[0] = data.data[i].X
                        obj[1] = data.data[i].Y
                        //往事先声明好的空数组push点位的x,y的坐标
                        SBINFO.push(obj)
                    }
                }
            }
        })



        marker, lineArr = xy;

        //创建地图
        var map = new AMap.Map("container", {
            resizeEnable: true,
            center: xy[0], //地图一加载进来的中心坐标
            zoom: 17
        });

        //创建人物行走标点
        marker = new AMap.Marker({
            map: map,
            position: xy[0],
            icon: new AMap.Icon({
                image: "images/zou.gif", //图片路径
                size: new AMap.Size(40, 40),    // 图标尺寸
                imageSize: new AMap.Size(40, 40)   // 根据所设置的大小拉伸或压缩图片
            }),//放入的是一个Icon对象
            offset: new AMap.Pixel(-14,-40), //上下移动调整图片在点位的位置
            autoRotation: true, //自动旋转
            angle:-90,
        });



        // 绘制轨迹
        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,
            // path: lineArr,
            strokeColor: "#AF5",  //线颜色
            // strokeOpacity: 1,     //线透明度
            strokeWeight: 6,      //线宽
            // strokeStyle: "solid"  //线样式
        });


        //开始标点
        start = new AMap.Marker({
            icon:  new AMap.Icon({
                image: "images/start.png",//图片路径
                size: new AMap.Size(40, 60),// 图标尺寸
                imageSize: new AMap.Size(40, 60)// 根据所设置的大小拉伸或压缩图片
            }),//放入的是一个Icon对象
            position: xy[0],//开始标点所在的位置
            offset: new AMap.Pixel(-20,-56)//上下移动调整图片在点位的位置
        });
        start.setMap(map);

        //结束标点
        stop = new AMap.Marker({
            icon:  new AMap.Icon({
                image: "images/stop.png",//图片路径
                size: new AMap.Size(40, 60),// 图标尺寸
                imageSize: new AMap.Size(40, 60)// 根据所设置的大小拉伸或压缩图片
            }),
            position: xy[xy.length-1],//结束标点所在的位置
            offset: new AMap.Pixel(-20,-56)
        });//放入的是一个Icon对象
        stop.setMap(map);


        //循环输出巡检点位位置
        for(var k = 0;k<SBINFO.length;k++){
            sbinfo = new AMap.Marker({
                icon:  new AMap.Icon({
                    image: "images/sb.png",
                    size: new AMap.Size(30, 30),
                    imageSize: new AMap.Size(30, 30)
                }),
                position: SBINFO[k],
                offset: new AMap.Pixel(-15,-20)
            });
            sbinfo.setMap(map);
        }



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


    //开始
    function startAnimation () {
        marker.moveAlong(lineArr, 200);
    }

    //暂停
    function pauseAnimation () {
        marker.pauseMove();
    }

    //继续
    function resumeAnimation () {
        marker.resumeMove();
    }

    //停止
    function stopAnimation () {
        marker.stopMove();
    }
</script>
</body>
</html>

原版高德地图轨迹代码:

<!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=1.4.15&key=c1c0d2288a11c68caff7c6bbc1c83672"></script>
<script>
    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: 17
    });

    marker = new AMap.Marker({
        map: map,
        position: [116.478935,39.997761],
        icon: "https://webapi.amap.com/images/car.png",
        offset: new AMap.Pixel(-26, -13),
        autoRotation: true,
        angle:-90,
    });

    // 绘制轨迹
    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,
        // path: lineArr,
        strokeColor: "#AF5",  //线颜色
        // strokeOpacity: 1,     //线透明度
        strokeWeight: 6,      //线宽
        // strokeStyle: "solid"  //线样式
    });


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

    map.setFitView();

    function startAnimation () {
        marker.moveAlong(lineArr, 200);
    }

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

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

    function stopAnimation () {
        marker.stopMove();
    }
</script>
</body>
</html>

参考高德地图官方示例:https://lbs.amap.com/demo/javascript-api/example/marker/replaying-historical-running-data

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇