openlayers 3 到曲线最近点
<!DOCTYPE html> <html> <head> <title>IGC Data</title> <link rel="stylesheet" href="http://openlayers.org/en/v3.13.0/css/ol.css" type="text/css"> <script src="http://openlayers.org/en/v3.13.0/build/ol.js"></script> </head> <body> <div id="map" class="map"></div> <input id="time" type="range" value="0" steps="1" /> <div id="info"> </div> <script> var colors = { 'Clement Latour': 'rgba(0, 0, 255, 0.7)', 'Damien de Baesnt': 'rgba(0, 215, 255, 0.7)', 'Sylvain Dhonneur': 'rgba(0, 165, 255, 0.7)', 'Tom Payne': 'rgba(0, 255, 255, 0.7)', 'Ulrich Prinz': 'rgba(0, 215, 255, 0.7)' }; var styleCache = {}; var styleFunction = function(feature) { var color = colors[feature.get('PLT')]; var style = styleCache[color]; if (!style) { style = new ol.style.Style({ stroke: new ol.style.Stroke({ color: color, width: 3 }) }); styleCache[color] = style; } return style; }; var vectorSource = new ol.source.Vector(); var igcUrls = [ 'data/igc/Clement-Latour.igc', 'data/igc/Damien-de-Baenst.igc', 'data/igc/Sylvain-Dhonneur.igc', 'data/igc/Tom-Payne.igc', 'data/igc/Ulrich-Prinz.igc' ]; function get(url, callback) { var client = new XMLHttpRequest(); client.open('GET', url); client.onload = function() { callback(client.responseText); }; client.send(); } var igcFormat = new ol.format.IGC(); for (var i = 0; i < igcUrls.length; ++i) { get(igcUrls[i], function(data) { var features = igcFormat.readFeatures(data, {featureProjection: 'EPSG:3857'}); vectorSource.addFeatures(features); }); } var time = { start: Infinity, stop: -Infinity, duration: 0 }; vectorSource.on('addfeature', function(event) { var geometry = event.feature.getGeometry(); time.start = Math.min(time.start, geometry.getFirstCoordinate()[2]); time.stop = Math.max(time.stop, geometry.getLastCoordinate()[2]); time.duration = time.stop - time.start; }); var map = new ol.Map({ layers: [ new ol.layer.Tile({ source: new ol.source.OSM({ attributions: [ new ol.Attribution({ html: 'All maps © ' + '<a href="http://www.opencyclemap.org/">OpenCycleMap</a>' }), ol.source.OSM.ATTRIBUTION ], url: 'http://{a-c}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png' }) }), new ol.layer.Vector({ source: vectorSource, style: styleFunction }) ], target: 'map', controls: ol.control.defaults({ attributionOptions: /** @type {olx.control.AttributionOptions} */ ({ collapsible: false }) }), view: new ol.View({ center: [703365.7089403362, 5714629.865071137], zoom: 9 }) }); var point = null; var line = null; var displaySnap = function(coordinate) { var closestFeature = vectorSource.getClosestFeatureToCoordinate(coordinate); var info = document.getElementById('info'); if (closestFeature === null) { point = null; line = null; info.innerHTML = ' '; } else { var geometry = closestFeature.getGeometry(); var closestPoint = geometry.getClosestPoint(coordinate); if (point === null) { point = new ol.geom.Point(closestPoint); } else { point.setCoordinates(closestPoint); } var date = new Date(closestPoint[2] * 1000); info.innerHTML = closestFeature.get('PLT') + ' (' + date.toUTCString() + ')'; var coordinates = [coordinate, [closestPoint[0], closestPoint[1]]]; if (line === null) { line = new ol.geom.LineString(coordinates); } else { line.setCoordinates(coordinates); } } map.render(); }; map.on('pointermove', function(evt) { if (evt.dragging) { return; } var coordinate = map.getEventCoordinate(evt.originalEvent); displaySnap(coordinate); }); map.on('click', function(evt) { displaySnap(evt.coordinate); }); var imageStyle = new ol.style.Circle({ radius: 5, fill: null, stroke: new ol.style.Stroke({ color: 'rgba(255,0,0,0.9)', width: 1 }) }); var strokeStyle = new ol.style.Stroke({ color: 'rgba(255,0,0,0.9)', width: 1 }); map.on('postcompose', function(evt) { var vectorContext = evt.vectorContext; if (point !== null) { vectorContext.setImageStyle(imageStyle); vectorContext.drawPointGeometry(point); } if (line !== null) { vectorContext.setFillStrokeStyle(null, strokeStyle); vectorContext.drawLineStringGeometry(line); } }); var featureOverlay = new ol.layer.Vector({ source: new ol.source.Vector(), map: map, style: new ol.style.Style({ image: new ol.style.Circle({ radius: 5, fill: new ol.style.Fill({ color: 'rgba(255,0,0,0.9)' }), stroke: null }) }) }); document.getElementById('time').addEventListener('input', function() { var value = parseInt(this.value, 10) / 100; var m = time.start + (time.duration * value); vectorSource.forEachFeature(function(feature) { var geometry = /** @type {ol.geom.LineString} */ (feature.getGeometry()); var coordinate = geometry.getCoordinateAtM(m, true); var highlight = feature.get('highlight'); if (highlight === undefined) { highlight = new ol.Feature(new ol.geom.Point(coordinate)); feature.set('highlight', highlight); featureOverlay.getSource().addFeature(highlight); } else { highlight.getGeometry().setCoordinates(coordinate); } }); map.render(); }); </script> </body> </html>
专业从事基于C#,WinForm ,WPF,Silverlight,WCF以及MS Sql Server 2000/2005/2008/2012 Oracle 9i/10g/11g数据库系统的ERP,CRM,企业进销存等各种数据库管理系统开发。Asp.net,Asp.net mvc,Webservice,WCF, Webapi等服务程序开发。
基于Oracle MySQL MSSql postgresql各种数据库的管理系统数据同步服务。以及基于MapXtreme, Arcgis Engine ,以及基于Arcgis for silverlight/Javascript的WebGIS等相关的GIS系统二次开发。基于Windows 10 Mobile的移动端开发方案。针对各种系统的二次开发维护,并提供相关开发的技术性支持,如程序BUG解决,应用系统架构,技术难题攻克等相关技术服务。
联系方式: QQ :80163278(devgis) 邮箱:devgis@qq.com