獲取json數(shù)據(jù)后在 地圖上打點(diǎn),根據(jù) json不斷移動(dòng)點(diǎn)的位置

2018-4-10    seo達(dá)人

如果您想訂閱本博客內(nèi)容,每天自動(dòng)發(fā)到您的郵箱中, 請(qǐng)點(diǎn)這里

<?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>點(diǎn)標(biāo)記</title>
    <link rel="stylesheet" />
    <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=您申請(qǐng)的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.');
    }); // 實(shí)例化點(diǎn)標(biāo)記 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; ?>

效果圖

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

去掉連續(xù)顯示多個(gè)點(diǎn),每次只顯示一個(gè)點(diǎn)

<?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>點(diǎn)標(biāo)記</title>
    <link rel="stylesheet" />
    <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=您申請(qǐng)的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.');
    }); // 實(shí)例化點(diǎn)標(biāo)記 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) { //在地圖上更新標(biāo)記
        // 自定義點(diǎn)標(biāo)記內(nèi)容 window.i+=10; var markerContent = document.createElement("div"); // 點(diǎn)標(biāo)記中的圖標(biāo) 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); // 點(diǎn)標(biāo)記中的文本 var markerSpan = document.createElement("span");
        markerSpan.className = 'marker';
        markerSpan.innerHTML = "Hi,我換新裝備啦!";
        markerContent.appendChild(markerSpan);

        marker.setContent(markerContent); //更新點(diǎn)標(biāo)記內(nèi)容 marker.setPosition([v1,v2]); //更新點(diǎn)標(biāo)記位置  } </script>
</body>
</html>

</script>
</body>
</html> _END; ?>

 

涉及setInterval傳參的問(wèn)題。

發(fā)現(xiàn)用addMarker(jdata.data[i].gpsx,jdata.data[i].gpsy);時(shí)程序可正常運(yùn)行,但是將該函數(shù) 放到setInterval中后卻出現(xiàn)了問(wèn)題,可通過(guò)閉包解決。

藍(lán)藍(lán)設(shè)計(jì)m.sillybuy.com )是一家專注而深入的界面設(shè)計(jì)公司,為期望卓越的國(guó)內(nèi)外企業(yè)提供卓越的UI界面設(shè)計(jì)、BS界面設(shè)計(jì) 、 cs界面設(shè)計(jì) 、 ipad界面設(shè)計(jì) 、 包裝設(shè)計(jì) 、 圖標(biāo)定制 、 用戶體驗(yàn) 、交互設(shè)計(jì)、 網(wǎng)站建設(shè) 、平面設(shè)計(jì)服務(wù)

分享本文至:

日歷

鏈接

個(gè)人資料

存檔