<!doctype html>
<html> <meta charset="utf-8"> <head> <title>轨迹回放</title> <script language="javascript" src="ol-debug.js"></script> </head> <body style="margin:0;" scroll="no"> <div id="map" style="height: 100%;width:100%;position: relative;"> <div style="width:190px;position: absolute;z-index: 11;right:10px;top:10px;background-color: #0D9BF2;opacity: 0.7;border-radius: 3px;padding: 10px;font-size: 12px"> <table> <tr> <td colspan="3">回放速度:<input type="range" id="speed" min="10" max="500" /></td> </tr> <tr> <td><button type="button" id="startButton" style="cursor: pointer;">开始回放</button></td> </tr> </table> </div> </div> <script type="text/javascript"> var projection = new ol.proj.Projection({ code: 'EPSG:4326', units: 'degrees' }); var center = [99.98270595073701,25.132418930530548]; var routeCoords = [[120.97202539443971,29.149083495140076],[120.97365617752077,29.147656559944153],[120.97478270530702,29.146594405174255], [120.97543716430665,29.14593994617462],[120.97596287727357,29.145285487174988],[120.9764349460602,29.144577383995056], [120.97669243812561,29.14408653974533],[120.97699284553528,29.143426716327667],[120.97723960876465,29.142654240131378], [120.97735226154329,29.142230451107025],[120.97756683826448,29.141243398189545],[120.97781896591188,29.140020310878754], [120.97790479660036,29.139483869075775],[120.97804427146912,29.138880372047424],[120.97839832305908,29.137893319129944], [120.97876310348511,29.137163758277893],[120.97941756248474,29.13626253604889],[120.9810483455658,29.134342074394226], [120.9818959236145,29.133376479148865],[120.98270595073701,29.132418930530548],[120.98334968090059,29.131678640842438], [120.98402559757234,29.130959808826447],[120.98470687866212,29.13033217191696],[120.985227227211,29.12989765405655], [120.9860908985138,29.129264652729034],[120.98707258701324,29.12864774465561],[120.9880542755127,29.12812203168869], [120.98936319351196,29.127537310123444],[120.99144458770752,29.126807749271393],[120.99297881126404,29.126287400722504], [120.99447548389435,29.125772416591644],[120.99569857120514,29.125321805477142],[120.99704504013062,29.124737083911896], [120.99830567836761,29.12410408258438],[120.99883675575256,29.123830497264862],[120.99963068962097,29.1233691573143], [121.00059628486633,29.122741520404816],[121.00166380405426,29.122038781642914],[121.00329995155334,29.120981991291046], [121.00475907325745,29.120016396045685],[121.00560128688812,29.119447767734528],[121.00612163543701,29.11910980939865], [121.0070389509201,29.11860018968582],[121.00769877433777,29.118267595767975],[121.00861608982086,29.1178759932518], [121.00979626178741,29.117489755153656],[121.01091742515564,29.117216169834137],[121.01166307926178,29.117071330547336], [121.01268768310547,29.116931855678562],[121.0139536857605,29.116878211498264],[121.01507484912872,29.116931855678562], [121.01689338684082,29.117071330547336],[121.01934492588043,29.117291271686558],[121.02029979228975,29.117350280284885], [121.02101325988771,29.117339551448826],[121.02191984653474,29.117242991924286],[121.02294981479646,29.117001593112946], [121.02402269840242,29.116583168506622],[121.02478981018068,29.1161647439003],[121.0260719060898,29.115327894687653]] var routeLength = routeCoords.length; var route = new ol.geom.LineString(routeCoords); var routeFeature = new ol.Feature({ type: 'route', geometry: route }); var geoMarker = new ol.Feature({ type: 'geoMarker', geometry: new ol.geom.Point(routeCoords[0]) }); var startMarker = new ol.Feature({ type: 'icon', geometry: new ol.geom.Point(routeCoords[0]) }); var endMarker = new ol.Feature({ type: 'icon', geometry: new ol.geom.Point(routeCoords[routeLength - 1]) }); var styles = { 'route': new ol.style.Style({ stroke: new ol.style.Stroke({ width: 6, color: [237, 212, 0, 0.8] }) }), 'icon': new ol.style.Style({ image: new ol.style.Icon({ anchor: [0.5, 1], src: 'https://openlayers.org/en/v4.6.5/examples/data/icon.png' }) }), 'geoMarker': new ol.style.Style({ image: new ol.style.Icon({ src: '/img/taxi.png', radius: 7, snapToPixel: false, fill: new ol.style.Fill({color: 'black'}), stroke: new ol.style.Stroke({ color: 'white', width: 2 }) }) }) }; var animating = false; var speed,now; var speedInput = document.getElementById('speed'); var startButton = document.getElementById('startButton'); var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [routeFeature, geoMarker, startMarker, endMarker] }), style: function(feature) { if (animating && feature.get('type') === 'geoMarker') { return null; } return styles[feature.get('type')]; } }); var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.XYZ({ url:'http://www.google.cn/maps/vt/pb=!1m4!1m3!1i{z}!2i{x}!3i{y}!2m3!1e0!2sm!3i345013117!3m8!2szh-CN!3scn!5e1105!12m4!1e68!2m2!1sset!2sRoadmap!4e0'//Roadmap SATELLITE satellite }), projection: 'EPSG:3857' }),vectorLayer], view: new ol.View({ projection: projection, center:center, zoom:14 }) }); 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 >= routeLength) { stopAnimation(true); return; } var currentPoint = new ol.geom.Point(routeCoords[index]); var feature = new ol.Feature(currentPoint); vectorContext.drawFeature(feature, styles.geoMarker); } // tell OpenLayers to continue the postcompose animation map.render(); }; function startAnimation() { if (animating) { stopAnimation(false); } else { animating = true; now = new Date().getTime(); speed = speedInput.value; startButton.textContent = '退出回放'; // hide geoMarker geoMarker.setStyle(null); // just in case you pan somewhere else map.getView().setCenter(center); map.on('postcompose', moveFeature); map.render(); } } /** * @param {boolean} ended end of animation. */ function stopAnimation(ended) { animating = false; startButton.textContent = '开始回放'; // if animation cancelled set the marker at the beginning var coord = ended ? routeCoords[routeLength - 1] : routeCoords[0]; /** @type {ol.geom.Point} */ (geoMarker.getGeometry()) .setCoordinates(coord); //remove listener map.un('postcompose', moveFeature); } </script> </body> </html>
openlayer 轨迹回放
最新推荐文章于 2024-09-27 10:38:43 发布