1. fatline ,这个名字也很有讲究,可是我还是不知道它的含义。
2. 创建 fatline 扩展类
1 var OPTIONS = { 2 altitude: 0 3 }; 4 5 class FatLine extends maptalks.BaSEObject { 6 constructor(linestring, options, material, layer) { 7 super(); 8 //geoutil.js getLinePosition 9 const { positions } = getLinePosition(linestring, layer); 10 const positions1 = _getLinePosition(linestring, layer).positions; 11 12 options = maptalks.Util.extend({}, OPTIONS, options, { layer, linestring, positions: positions1 }); 13 this._initOptions(options); 14 15 const geometry = new THREE.LineGeometry(); 16 geometry.setPositions(positions); 17 const map = layer.getMap(); 18 const size = map.getSize(); 19 const width = size.width, 20 height = size.height; 21 material.resolution.set(width, height); 22 const line = new THREE.Line2(geometry, material); 23 line.computeLinedistances(); 24 this._createGroup(); 25 this.getobject3d().add(line); 26 const { altitude } = options; 27 const z = layer.distancetoVector3(altitude, altitude).x; 28 const center = linestring.getCenter(); 29 const v = layer.coordinatetoVector3(center, z); 30 this.getobject3d().position.copy(v); 31 } 32 33 34 setSymbol(material) { 35 if (material && material instanceof THREE.Material) { 36 material.needsUpdate = true; 37 const size = this.getMap().getSize(); 38 const width = size.width, 39 height = size.height; 40 material.resolution.set(width, height); 41 this.getobject3d().children[0].material = material; 42 } 43 return this; 44 } 45 46 //test BaSEObject customize its identity 47 identify(coordinate) { 48 const layer = this.getLayer(), size = this.getMap().getSize(), 49 camera = this.getLayer().getCamera(), positions = this.getoptions().positions, altitude = this.getoptions().altitude; 50 let canvas = layer._testCanvas; 51 if (!canvas) { 52 canvas = layer._testCanvas = document.createElement('canvas'); 53 } 54 canvas.width = size.width; 55 canvas.height = size.height; 56 const context = canvas.getContext('2d'); 57 58 const pixels = simplepath.vectors2Pixel(positions, size, camera, altitude, layer); 59 const linewidth = this.getobject3d().children[0].material.linewidth + 3; 60 simplepath.draw(context, pixels, 'Linestring', { linewidth: linewidth }); 61 const pixel = this.getMap().coordToContainerPoint(coordinate); 62 if (context.isPointInstroke(pixel.x, pixel.y)) { 63 return true; 64 } 65 } 66 }
3. 将 ./data/berlin-roads.txt 数据进行解密并进行处理,获取路径经纬度。
1 fetch('./data/berlin-roads.txt').then(function (res) { 2 return res.text(); 3 }).then(function (geojson) { 4 geojson = LZString.decompressFromBase64(geojson); 5 geojson = JSON.parse(geojson); 6 7 var linestrings = maptalks.GeoJSON.toGeometry(geojson); 8 var timer = 'generate line time'; 9 console.time(timer); 10 var list = []; 11 linestrings.forEach(function (linestring) { 12 list.push({ 13 linestring, 14 //geoutil.js lineLength 15 len: lineLength(linestring) 16 }); 17 }); 18 list = list.sort(function (a, b) { 19 return b.len - a.len 20 }); 21 lines = list.slice(0, 200).map(function (d) { 22 var line = new FatLine(d.linestring, { altitude: 100 }, material, threeLayer); 23 24 //tooltip test 25 line.setToolTip(line.getId(), { 26 showTimeout: 0, 27 eventsPropagation: true, 28 dx: 10 29 }); 30 31 32 //infowindow test 33 line.setInfoWindow({ 34 content: 'hello world,id:' + line.getId(), 35 title: 'message', 36 animationDuration: 0, 37 autoOpenOn: false 38 }); 39 40 41 //event test 42 ['click', 'mouSEOut', 'mouSEOver', 'mousedown', 'mouseup', 'dblclick', 'contextmenu'].forEach(function (eventType) { 43 line.on(eventType, function (e) { 44 console.log(e.type, e); 45 // console.log(this); 46 if (e.type === 'mouSEOut') { 47 this.setSymbol(material); 48 } 49 if (e.type === 'mouSEOver') { 50 this.setSymbol(highlightmaterial); 51 } 52 }); 53 }); 54 return line; 55 }); 56 console.log('lines.length:', lines.length); 57 console.timeEnd(timer); 58 threeLayer.addMesh(lines); 59 threeLayer.config('animation', true); 60 })
5. 源码地址
https://github.com/WhatGIS/maptalkMap/tree/main/threelayer/demo
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。