记录基于geoserver地图服务,Openlayers3在web前端实现车辆轨迹回放功能,并记录和解决过程中出现的linestring只描绘部分经纬度坐标问题。
参考Openlayers3 官网例子
html
<!DOCTYPE html> <html lang="en"> <head> <title>车辆轨迹回放</title> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <link rel="stylesheet" href="../css/bootstrap.min.css"/> <link rel="stylesheet" href="../ol/ol.css"/> <style> #map { position: relative; } .popover{ min-width: 60px; } html{height:100%} body{height:100%;margin:0px;padding:0px} </style> </head> <body style="margin: 0px 0px 0px 0px;line-height:0px;"> <div id="content"> <!--<div id="map" style="height: 100%;width:100%"></div>--> <div class="row-fluid"> <div> <div id="map" class="map"></div> </div> </div> <div class="row-fluid"> <div class="span3" style="position:absolute;top:0px;right:0px;"> <div class="accordion-group widget-box"> <div class="accordion-heading"> <div class="widget-title"><a data-parent="#collapse-group" href="#collapseGOne" data-toggle="collapse"><span class="icon"><i class="icon-map-marker"></i></span> <h5>轨迹查询</h5> </a> </div> </div> <div class="accordion-body in collapse" id="collapseGOne"> <div class="form-actions"> <div class="control-group" style="margin-bottom: 0px"> <label class="control-label"><i class="icon-truck"></i>设备</label> <div class="controls"> <select id="busSelect" class="span10"> <option value="*">请选择设备</option> </select> </div> </div> </div> <div class="form-actions"> <div class="control-group" style="margin-bottom: 0px"> <label class="control-label"><i class="icon-table"></i>日期</label> <div class="controls"> <div data-date="" class="input-append date datepicker"> <input id="traceday" type="text" data-date-format="yyyy-mm-dd" class="span10" disabled> <span class="add-on"><i class="icon-time"></i></span></div> </div> </div> </div> <div style="padding: 19px 20px 20px; margin-top: 20px; margin-bottom: 20px;"> <div class="control-group" style="margin-bottom: 0px"> <button id="queryBtn" class="btn btn-primary"><i class="icon-search"></i> 轨迹查询</button> <span class="remind"></span> </div> <div class="control-group" style="margin-top: 10px"> <button id="animateBtn" class="btn btn-info"><i class="icon-eye-open"></i> 轨迹回放</button> <input id="speed" type="range" min="1" max="100" step="10" value="10"> <span><i class="icon-cog">速度</i></span> </div> </div> </div> </div> </div> </div> </div> </div> <script src="/UploadFiles/2021-04-02/jquery.min.js">map初始化
$(document).ready(function () { map = new ol.Map({ logo: false, target: document.getElementById('map'), layers: layers, view: view }); initSelect();//设备下拉框列表初始化 //时间控件初始化 $('#s-traceday').val(transfromTime(new Date(),false)+" 00:00:00"); $('#e-traceday').val(transfromTime(new Date(),true)); $('.datepicker').datetimepicker({ minView:0, format: 'yyyy-MM-dd hh:mm:ss', todayBtn : "linked", autoclose : true, todayHighlight : true, startDate: daylimit, endDate:'+1d'//结束时间,在这时间之后都不可选 }); }); //轨迹line layer var vsource = new ol.source.Vector({ type: 'LineString', features: [] }); var linelayers = new ol.layer.Vector({ source: vsource, style: new ol.style.Style({ fill: new ol.style.Fill({ color: '#0044CC' }), stroke: new ol.style.Stroke({ color: '#0044CC', width: 4 }) }) }); //地图基础参数 var map; var center = [121.6606763113213, 31.14611063632111]; var lng, lat; var source = new ol.source.Vector({ wrapX: false });; var projection = new ol.proj.Projection({ code: 'EPSG:4326', units: 'degrees', axisOrientation: 'neu' }); var view = new ol.View({ projection: projection, center: center, zoom: 16 }); var layers = [new ol.layer.Tile({ title: '、地图', visible: true, preload: Infinity, source: new ol.source.TileWMS({ url: gisUrl, params: { 'VERSION': '1.1.0', tiled: true, STYLES: '', LAYERS: 'shanghai:maptest', } }) }),linelayers];ajax获取坐标数据
//轨迹查询按钮点击 var positions=[]; $("#queryBtn").click(function(){ //清除之前的图层 if (centerSource.getFeatures != null) { centerSource.clear(); } linelayers.getSource().clear(true); positions=[];//清空 //取值 var busnum=$("#busSelect").val(); var traceday=$("#traceday").val(); if(busnum=="*"){ $(".remind").html('<i class="icon-info-sign">请先选择车辆</i>'); return; }else{ $(".remind").html(''); busnum=busnum.slice(2); } if(transfromTime(new Date(),false)==traceday){//当天 traceday="*"; }else{ traceday=traceday.replace(/(-)/g,""); } $(".remind").html('<i class="icon-cogs"> 正在查询...</i>'); //请求 $.getJSON(baseUrl+"trace/query/"+busnum+"/"+traceday,"",function(data){ if(data.length==0){ $(".remind").html('<i class="icon-info-sign">未查到gps数据</i>');return; } var position = []; for(var i = 0;i<data.length;i++){ if(i!=0&&data[i].lon==data[i-1].lon&&data[i].lon==data[i-1].lon){//去除重复数据 continue; } position = [parseFloat(data[i].lon),parseFloat(data[i].lat)]; positions.push(position); } console.log(positions); if(positions.length==1){ $(".remind").html('<i class="icon-info-sign">该车辆当天停在该位置未启动</i>'); centerAt(positions[0]); return; } AddLayer(positions); }); });显示轨迹
//轨迹描绘 function AddLayer() { var lineFeature = new ol.Feature({//路线 geometry: new ol.geom.LineString(positions,'XY'), }); linelayers.getSource().addFeature(lineFeature); var startFeature = new ol.Feature({//起点 geometry: new ol.geom.Point(positions[0]), population: 4000, rainfall: 500 }); startFeature.setStyle(startStyle); linelayers.getSource().addFeature(startFeature); var endFeature = new ol.Feature({//终点 geometry: new ol.geom.Point(positions[positions.length-1]), population: 4000, rainfall: 500 }); endFeature.setStyle(endStyle); linelayers.getSource().addFeature(endFeature); carFeature = new ol.Feature({//车子 geometry: new ol.geom.Point(positions[0]), population: 4000, rainfall: 500 }); carFeature.setStyle(carStyle); linelayers.getSource().addFeature(carFeature); var extent = linelayers.getSource().getExtent();//合适比例缩放居中 view.fit(extent, map.getSize()); }显示单点
//居中 车辆不运动时居中显示图标处理 var centerLayer = null; var centerSource = new ol.source.Vector({ features: null }); //居中在一个位置 function centerAt(position) { var pan = ol.animation.pan({ duration: 2000, source: (view.getCenter()) }); view.setCenter(position); var iconFeature = new ol.Feature({ geometry: new ol.geom.Point(position), name: 'Null Island', population: 4000, rainfall: 500 }); iconFeature.setStyle(iconStyle); centerSource.addFeature(iconFeature); centerLayer = new ol.layer.Vector({ source: centerSource }); map.addLayer(centerLayer); centerLayer.setVisible(true); }点在线上运动,轨迹回放
//轨迹回放start 参考官网 var carFeature = null; var speed, now; var animating = false; $("#animateBtn").click(function(){ if(positions.length==0){ $(".remind").html('<i class="icon-info-sign">请先查询轨迹</i>'); return; } if (animating) { stopAnimation(false); } else { animating = true; now = new Date().getTime(); speed = $("#speed").val();//速度 $("#animateBtn").html('<i class="icon-eye-close"></i> 取消回放'); carFeature.setStyle(null); // map.getView().setCenter(center); map.on('postcompose', moveFeature); map.render(); } }); var moveFeature = function(event) { var vectorContext = event.vectorContext; var frameState = event.frameState; if (animating) { var elapsedTime = frameState.time - now; // here the trick to increase speed is to jump some indexes // on lineString coordinates var index = Math.round(speed * elapsedTime / 1000); if (index >= positions.length) { stopAnimation(true); return; } var currentPoint = new ol.geom.Point(positions[index]); var feature = new ol.Feature(currentPoint); vectorContext.drawFeature(feature, carStyle); } // tell OL3 to continue the postcompose animation map.render(); }; function startAnimation() { if (animating) { stopAnimation(false); } else { animating = true; now = new Date().getTime(); speed = speedInput.value; $("#animateBtn").html('<i class="icon-eye-close"></i> 取消回放'); // hide geoMarker geoMarker.setStyle(null); // just in case you pan somewhere else map.getView().setCenter(center); map.on('postcompose', moveFeature); map.render(); } } function stopAnimation(ended) { animating = false; $("#animateBtn").html('<i class="icon-eye-open"></i> 轨迹回放'); // if animation cancelled set the marker at the beginning var coord = ended "htmlcode">var lineFeature = new ol.Feature({//路线 geometry: new ol.geom.LineString(positions,'XY'), }); linelayers.getSource().addFeature(lineFeature);调用这段代码显示轨迹时,从数据库取到20个坐标,就可能只显示4个坐标,本来是弯曲的轨迹,但是实际上就是折线,很尴尬。
误打误撞,和同学 交流过程中发现问题所在,特此感谢。
在ajax获取坐标数据中发现问题所在:
原先错误代码position = [data[i].lon,data[i].lat]; positions.push(position);正确代码
position = [parseFloat(data[i].lon),parseFloat(data[i].lat)]; positions.push(position);原因就是没有把float类型的坐标利用parseFloat强转,导致默认的泛数据类型精确度不够,经纬度小数点后末尾几位就会被忽略,于是造成数据失效,描出的线就会有问题。
附上icon、起点、终点、车辆等地图样式
//样式,供上述代码调用 var iconStyle = new ol.style.Style({ image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({ anchor: [0.5, 0.8], anchorXUnits: 'fraction', anchorYUnits: 'pixels', opacity: 0.75, src: 'img/iconfont-weizhi-red.png' })) }); var startStyle = new ol.style.Style({ image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({ anchor: [0.5, 0.8], opacity: 0.8, src: 'img/start.png' /*anchorXUnits: 'fraction', anchorYUnits: 'pixels', opacity: 0.75,*/ })) }); var endStyle = new ol.style.Style({ image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({ src: 'img/end.png', anchor: [0.5, 0.8], })) }); var carStyle = new ol.style.Style({ image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({ src: 'img/car.png', anchor: [0.5, 0.8], })) });以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]