|
49 | 49 | <label for="pointCount">点数量:</label>
|
50 | 50 | <input id="pointCount" type="text" value="500" />
|
51 | 51 | <input id="searchButton" type="submit" value="查询" />
|
52 |
| - <input id="prevButton" type="button" value="前一天" /> |
53 |
| - <input id="nextButton" type="button" value="后一天" /> |
| 52 | + <input id="prevDateButton" type="button" value="前一天" /> |
| 53 | + <input id="nextDateButton" type="button" value="后一天" /> |
| 54 | + <input id="prevPageButton" type="button" value="上一页" /> |
| 55 | + <input id="nextPageButton" type="button" value="下一页" /> |
54 | 56 | </form>
|
55 |
| - <div id="log" class="log"></div> |
| 57 | + <div id="log" class="log"> |
| 58 | + </div> |
56 | 59 | </div>
|
57 | 60 | <div class="container" id="baiduMapCtn"></div>
|
58 | 61 | <script type="text/javascript" src="//api.map.baidu.com/api?v=3.0&ak=XwGhtOZnTOQk7lFssFiI1GR3"></script>
|
|
101 | 104 | }
|
102 | 105 | function splitDatapointsByVelocity(dataPoints){
|
103 | 106 | let splitedPoints = [];
|
| 107 | + splitedPoints.count = dataPoints.length; |
| 108 | + splitedPoints.startTime = dataPoints[0].at; |
| 109 | + splitedPoints.endTime = dataPoints[dataPoints.length - 1].at; |
104 | 110 | let currentVelocityGroup,
|
105 | 111 | previousVelocityGroup = getVelocityGroup(calcVelocity(dataPoints[0], dataPoints[1])),
|
106 | 112 | tempPoints = {
|
|
132 | 138 | var $startTime = $('startTime');
|
133 | 139 | var $endTime = $('endTime');
|
134 | 140 | var $pointCount = $('pointCount');
|
135 |
| - var $prevButton = $('prevButton'); |
136 |
| - var $nextButton = $('nextButton'); |
| 141 | + var $prevDateButton = $('prevDateButton'); |
| 142 | + var $nextDateButton = $('nextDateButton'); |
| 143 | + var $prevPageButton = $('prevPageButton'); |
| 144 | + var $nextPageButton = $('nextPageButton'); |
137 | 145 | var $searchButton = $('searchButton');
|
138 | 146 | var $log = $('log');
|
139 | 147 | $('baiduMapCtn').style.height = (document.body.offsetHeight - $('head').offsetHeight) + 'px'
|
140 | 148 | function CarMarker(deviceId, start, end){
|
141 | 149 | var _this = this;
|
142 | 150 | this.start = start;
|
143 | 151 | this.end = end;
|
| 152 | + this.pointsCache = {}; |
| 153 | + this.cursorListOfPageIndex = [1]; |
| 154 | + this.currentPageIndex = 0; |
144 | 155 | var api = new OneNetApi($apiKey.value);
|
145 | 156 | this._api = api;
|
146 | 157 | api.getDeviceInfo(deviceId).then(function(res){
|
|
150 | 161 | });
|
151 | 162 | }
|
152 | 163 | CarMarker.prototype.showHistory = function(deviceId){
|
| 164 | + var _this = this; |
153 | 165 | this._api.getDataPoints(deviceId, {datastream_id:'Gps', start: this.start, end: this.end, limit: $pointCount.value}).then(function(res){
|
154 | 166 | console.log('api调用完成,服务器返回data为:', res);
|
155 |
| - $log.innerHTML = '本次共渲染' + res.data.count + '个点'; |
156 |
| - pageControl.baiduMap.resetMarker(splitDatapointsByVelocity(res.data.datastreams[0].datapoints)); |
| 167 | + $log.innerHTML = '当前第1页,本次共渲染' + res.data.count + '个点'; |
| 168 | + if(res.data.cursor){ //加入第二页的corsor |
| 169 | + _this.cursorListOfPageIndex[1] = res.data.cursor; |
| 170 | + } |
| 171 | + var splitedPoints = splitDatapointsByVelocity(res.data.datastreams[0].datapoints); |
| 172 | + pageControl.baiduMap.resetMarker(splitedPoints); |
| 173 | + _this.pointsCache[1] = splitedPoints; |
| 174 | + }); |
| 175 | + } |
| 176 | + CarMarker.prototype.showDataByPageIndex = function(pageIndex){ |
| 177 | + var cursor = this.cursorListOfPageIndex[pageIndex]; |
| 178 | + if(!cursor){ |
| 179 | + $log.innerHTML = '当前第' + (pageIndex + 1) + '页,本次共渲染0个点'; |
| 180 | + return; |
| 181 | + } |
| 182 | + var splitedPoints = this.pointsCache[cursor]; |
| 183 | + if(splitedPoints){ |
| 184 | + pageControl.baiduMap.resetMarker(splitedPoints); |
| 185 | + $log.innerHTML = '当前第' + (pageIndex + 1) + '页,本次共渲染' + splitedPoints.count + '个点'; |
| 186 | + return; |
| 187 | + } |
| 188 | + var _this = this; |
| 189 | + this._api.getDataPoints($deviceId.value, {datastream_id:'Gps', start: this.start, end: this.end, limit: $pointCount.value, cursor: cursor}).then(function(res){ |
| 190 | + if(res.data.cursor){ //加入下一页的corsor |
| 191 | + _this.cursorListOfPageIndex[pageIndex + 1] = res.data.cursor; |
| 192 | + } |
| 193 | + var splitedPoints = splitDatapointsByVelocity(res.data.datastreams[0].datapoints); |
| 194 | + pageControl.baiduMap.resetMarker(splitedPoints); |
| 195 | + _this.pointsCache[cursor] = splitedPoints; |
| 196 | + $log.innerHTML = '当前第' + (pageIndex + 1) + '页,本次共渲染' + splitedPoints.count + '个点'; |
157 | 197 | });
|
158 | 198 | }
|
| 199 | + CarMarker.prototype.renderPrevPage = function(){ |
| 200 | + if(this.currentPageIndex == 0){ |
| 201 | + return; |
| 202 | + } |
| 203 | + this.showDataByPageIndex(--this.currentPageIndex); |
| 204 | + }; |
| 205 | + CarMarker.prototype.renderNextPage = function(){ |
| 206 | + this.showDataByPageIndex(++this.currentPageIndex); |
| 207 | + }; |
159 | 208 | var pageControl = {
|
160 | 209 | init: function(){
|
161 |
| - this.baiduMapCtn = document.getElementById("baiduMapCtn"); |
| 210 | + this.baiduMapCtn = document.getElementById('baiduMapCtn'); |
162 | 211 | this.baiduMap.init(this.baiduMapCtn);
|
163 | 212 | var _this = this;
|
164 | 213 | this.initTimeRound(new Date());
|
165 |
| - if(localStorage.getItem('apiKey')){ |
166 |
| - //0XlwMJm8U42KEZ394N4p8hm2p=s= |
| 214 | + if(localStorage.getItem('apiKey')){//0XlwMJm8U42KEZ394N4p8hm2p=s= |
167 | 215 | $apiKey.value = localStorage.getItem('apiKey');
|
168 | 216 | }
|
| 217 | + if(localStorage.getItem('deviceId')){//517162506 |
| 218 | + $deviceId.value = localStorage.getItem('deviceId'); |
| 219 | + } |
| 220 | + if(localStorage.getItem('pointCount')){//500 |
| 221 | + $pointCount.value = localStorage.getItem('pointCount'); |
| 222 | + } |
169 | 223 | $searchButton.onclick = function(e){
|
170 | 224 | if($apiKey.value.trim()){
|
171 | 225 | localStorage.setItem('apiKey', $apiKey.value.trim());
|
172 | 226 | }
|
| 227 | + if($deviceId.value.trim()){ |
| 228 | + localStorage.setItem('deviceId', $deviceId.value.trim()); |
| 229 | + } |
| 230 | + if($pointCount.value.trim()){ |
| 231 | + localStorage.setItem('pointCount', $pointCount.value.trim()); |
| 232 | + } |
173 | 233 | e.preventDefault();
|
174 |
| - new CarMarker($deviceId.value, $startTime.value, $endTime.value); |
| 234 | + _this.carMarker = new CarMarker($deviceId.value, $startTime.value, $endTime.value); |
175 | 235 | }
|
176 |
| - $prevButton.onclick = function(){ |
| 236 | + $prevDateButton.onclick = function(){ |
177 | 237 | var dateCurrent = new Date($startTime.value);
|
178 | 238 | var dateNew = new Date(+dateCurrent - 3600 * 1000 * 24);
|
179 | 239 | _this.initTimeRound(dateNew);
|
180 | 240 | $searchButton.click();
|
181 | 241 | }
|
182 |
| - $nextButton.onclick = function(){ |
183 |
| - var dateCurrent = new Date($startTime.value); |
184 |
| - var dateNew = new Date(+dateCurrent + 3600 * 1000 * 24); |
185 |
| - _this.initTimeRound(dateNew); |
186 |
| - $searchButton.click(); |
| 242 | + $nextDateButton.onclick = function(){ |
| 243 | + _this.carMarker.renderPrevPage(); |
| 244 | + } |
| 245 | + $prevPageButton.onclick = function(){ |
| 246 | + _this.carMarker.renderPrevPage(); |
| 247 | + } |
| 248 | + $nextPageButton.onclick = function(){ |
| 249 | + _this.carMarker.renderNextPage(); |
187 | 250 | }
|
188 | 251 | },
|
189 | 252 | initTimeRound: function(date){
|
|
214 | 277 | resetMarker: function(splitedPoints){
|
215 | 278 | this.map.clearOverlays();
|
216 | 279 | var _this = this;
|
217 |
| - console.log(splitedPoints) |
218 | 280 | splitedPoints.forEach(item => {
|
219 | 281 | _this.drawLine(item.points, VelocityGroupColor[item.velocityGroup]);
|
220 | 282 | });
|
|
231 | 293 | });
|
232 | 294 | var endPoints = splitedPoints[splitedPoints.length - 1].points;
|
233 | 295 | var markerEnd = new BMap.Marker(endPoints[endPoints.length - 1], {icon:iconEnd});
|
| 296 | + markerStart.setLabel(new BMap.Label(splitedPoints.startTime, {offset: new BMap.Size(-20,-20)})); |
| 297 | + markerEnd.setLabel(new BMap.Label(splitedPoints.endTime, {offset: new BMap.Size(-20,-20)})); |
234 | 298 | this.map.addOverlay(markerStart);
|
235 | 299 | this.map.addOverlay(markerEnd);
|
| 300 | + this.map.setViewport([splitedPoints[0].points[0], endPoints[endPoints.length - 1]]); |
236 | 301 | },
|
237 | 302 | drawLine: function(pointsArr, color){
|
238 | 303 | var sy = new BMap.Symbol(BMap_Symbol_SHAPE_BACKWARD_OPEN_ARROW, {
|
|
250 | 315 | strokeColor: color //折线颜色
|
251 | 316 | });
|
252 | 317 | this.map.addOverlay(polyline);
|
253 |
| - this.map.centerAndZoom(pointsArr[0], 15); |
254 | 318 | }
|
255 | 319 | }
|
256 | 320 | };
|
|
0 commit comments