zoukankan      html  css  js  c++  java
  • 获取json数据后在 地图上打点,根据 json不断移动点的位置

    <?php
    
    echo <<<_END
    <!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="http://cache.amap.com/lbs/static/main1119.css"/>
        <style>
            .marker {
                color: #ff6600;
                padding: 4px 10px;
                border: 1px solid #fff;
                white-space: nowrap;
                font-size: 12px;
                font-family: "";
                background-color: #0066ff;
            }
        </style>
        <script src="http://webapi.amap.com/maps?v=1.3&key=您申请的key值"></script>
        <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
    </head>
    <body>
    <div id="container"></div>
    <script>
        var marker, map = new AMap.Map("container", {
            resizeEnable: true,
            center: [126.60580555556, 45.702363888889],
            zoom: 13
        });
        var getJSON = function(url) {
          return new Promise(function(resolve, reject) {
            var xhr = new XMLHttpRequest();
            xhr.open('get', url, true);
            xhr.responseType = 'json';
            xhr.onload = function() {
              var status = xhr.status;
              if (status == 200) {
                resolve(xhr.response);
              } else {
                reject(status);
              }
            };
            xhr.send();
          });
        };
    
        getJSON('http://web.cellpies.com/api/driving/getVehicleLocationPoints?vehicleDeviceId=0400000000030603&timeType=4&startTime=2017-03-17%2013:00:00&stopTime=2017-03-17%2014:00:00').then(function(jdata) {
            //alert('Your Json result is:  ' + jdata); //you can comment this, i used it to debug
            //alert(jdata.data[0].gpsx);
            //alert(jdata.data[0].gpsy);
            window.i=0;
            //addMarker(jdata.data[i].gpsx,jdata.data[i].gpsy);
            setInterval(function () { addMarker(jdata.data[window.i].gpsx,jdata.data[window.i].gpsy); },"1000");
           
        }, function(status) { //error detection....
          alert('Something went wrong.');
        });
        
        // 实例化点标记
     function addMarker(v1,v2) {
         
         window.i+=10;
            marker = new AMap.Marker({
                icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
                position: [v1,v2]
            });
            marker.setMap(map);
        }
    
    </script>
    </body>
    </html>
    
    </script>
    </body>
    </html>
    
    _END;
     
    
    ?>

    效果图

    -----------------------------------------------------------------------更新---------------------------------------------------------

    去掉连续显示多个点,每次只显示一个点

    <?php
    echo<<<_END
    <!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="http://cache.amap.com/lbs/static/main1119.css"/>
        <style>
            .marker {
                color: #ff6600;
                padding: 4px 10px;
                border: 1px solid #fff;
                white-space: nowrap;
                font-size: 12px;
                font-family: "";
                background-color: #0066ff;
            }
        </style>
        <script src="http://webapi.amap.com/maps?v=1.3&key=您申请的key值"></script>
        <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
    </head>
    <body>
    <div id="container"></div>
    <script>
        var marker, map = new AMap.Map("container", {
            resizeEnable: true,
            center: [126.60580555556, 45.702363888889],
            zoom: 13
        });
        var getJSON = function(url) {
          return new Promise(function(resolve, reject) {
            var xhr = new XMLHttpRequest();
            xhr.open('get', url, true);
            xhr.responseType = 'json';
            xhr.onload = function() {
              var status = xhr.status;
              if (status == 200) {
                resolve(xhr.response);
              } else {
                reject(status);
              }
            };
            xhr.send();
          });
        };
    
        getJSON('http://web.cellpies.com/api/driving/getVehicleLocationPoints?vehicleDeviceId=0400000000030603&timeType=4&startTime=2017-03-17%2013:00:00&stopTime=2017-03-17%2014:00:00').then(function(jdata) {
            addMarker(jdata.data[0].gpsx,jdata.data[0].gpsy);
            window.i=1;
            //updateMarker(jdata.data[1].gpsx,jdata.data[1].gpsy);
            setInterval(function () { updateMarker(jdata.data[window.i].gpsx,jdata.data[window.i].gpsy); },"1000");
           
        }, function(status) { //error detection....
          alert('Something went wrong.');
        });
        
        // 实例化点标记
     function addMarker(v1,v2) {
         
            marker = new AMap.Marker({
                icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
                position: [v1,v2]
            });
            marker.setMap(map);
        }
          function updateMarker(v1,v2) {
            //在地图上更新标记
            // 自定义点标记内容
            window.i+=10;
            var markerContent = document.createElement("div");
    
            // 点标记中的图标
            var markerImg = document.createElement("img");
            markerImg.className = "markerlnglat";
            markerImg.src = "http://webapi.amap.com/theme/v1.3/markers/n/mark_r.png";
            markerContent.appendChild(markerImg);
    
            // 点标记中的文本
            var markerSpan = document.createElement("span");
            markerSpan.className = 'marker';
            markerSpan.innerHTML = "Hi,我换新装备啦!";
            markerContent.appendChild(markerSpan);
    
            marker.setContent(markerContent); //更新点标记内容
            marker.setPosition([v1,v2]); //更新点标记位置
        }
    
    </script>
    </body>
    </html>
    
    </script>
    </body>
    </html>
    
    _END;
    ?>

    涉及setInterval传参的问题。

    发现用addMarker(jdata.data[i].gpsx,jdata.data[i].gpsy);时程序可正常运行,但是将该函数 放到setInterval中后却出现了问题,可通过闭包解决。

  • 相关阅读:
    Http Requests for PHP
    关于ORA-00979 不是 GROUP BY 表达式错误的解释
    boke例子: freermarker:在使用ajax传递json数据的时候多出冒号
    boke练习: springboot整合springSecurity出现的问题,传递csrf
    boke练习: springboot整合springSecurity出现的问题,post,delete,put无法使用
    feign三:覆写feign的默认配置及feign的日志
    boke练习: spring boot: security post数据时,要么关闭crst,要么添加隐藏域
    boke练习: freemarker对空变量报错 (classic_compatible设置true,解决报空错误)
    mysql查询、子查询、连接查询
    MySQL Group By 实例讲解(二)
  • 原文地址:https://www.cnblogs.com/vactor/p/6681196.html
Copyright © 2011-2022 走看看