|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <title>Custom popup</title> |
| 5 | + <meta charset="UTF-8"> |
| 6 | + <link rel="stylesheet" type="text/css" href="css/example.css"> |
| 7 | + <link rel="stylesheet" type="text/css" href="css/LoadingScreen.css"> |
| 8 | + |
| 9 | + <style> |
| 10 | + .bubble { |
| 11 | + background-color: #fff; |
| 12 | + border-radius: 10px; |
| 13 | + text-align: center; |
| 14 | + padding: 10px 15px; |
| 15 | + box-shadow: 0 0.125rem 0.5rem rgba(0, 0, 0, .3), 0 0.0625rem 0.125rem rgba(0, 0, 0, .2); |
| 16 | + } |
| 17 | + .pointer { |
| 18 | + height: 12px; |
| 19 | + width: 12px; |
| 20 | + background-color: #fff; |
| 21 | + transform: translate(-50%, -50%) rotate(45deg); |
| 22 | + position: relative; |
| 23 | + left: 80%; |
| 24 | + } |
| 25 | + </style> |
| 26 | + |
| 27 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 28 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js"></script> |
| 29 | +</head> |
| 30 | +<body> |
| 31 | +<div id="description"> |
| 32 | + Clicking a position while holding `p` key down will pop a custom popup. |
| 33 | +</div> |
| 34 | +<div id="viewerDiv" class="viewer"></div> |
| 35 | + |
| 36 | +<script src="js/GUI/GuiTools.js"></script> |
| 37 | +<script src="../dist/itowns.js"></script> |
| 38 | +<script src="../dist/debug.js"></script> |
| 39 | +<script src="js/GUI/LoadingScreen.js"></script> |
| 40 | +<script src="js/plugins/FeatureToolTip.js"></script> |
| 41 | +<script type="text/javascript"> |
| 42 | + /* global itowns */ |
| 43 | + |
| 44 | + // Setup view and layers |
| 45 | + const placement = { |
| 46 | + coord: new itowns.Coordinates('EPSG:4326', 3.5, 44), |
| 47 | + range: 1000000, |
| 48 | + }; |
| 49 | + const viewerDiv = document.getElementById('viewerDiv'); |
| 50 | + const view = new itowns.GlobeView(viewerDiv, placement); |
| 51 | + var menuGlobe = new GuiTools('menuDiv', view); |
| 52 | + itowns.Fetcher.json('./layers/JSONLayers/Ortho.json').then(function _(config) { |
| 53 | + config.source = new itowns.WMTSSource(config.source); |
| 54 | + view.addLayer(new itowns.ColorLayer('Ortho', config)); |
| 55 | + }); |
| 56 | + function addElevationLayerFromConfig(config) { |
| 57 | + config.source = new itowns.WMTSSource(config.source); |
| 58 | + view.addLayer(new itowns.ElevationLayer(config.id, config)); |
| 59 | + } |
| 60 | + itowns.Fetcher.json('./layers/JSONLayers/WORLD_DTM.json').then(addElevationLayerFromConfig); |
| 61 | + itowns.Fetcher.json('./layers/JSONLayers/IGN_MNT_HIGHRES.json').then(addElevationLayerFromConfig); |
| 62 | + debug.createTileDebugUI(menuGlobe.gui, view); |
| 63 | + |
| 64 | + // Create a custom div which will be displayed as a label |
| 65 | + const customDiv = document.createElement('div'); |
| 66 | + const bubble = document.createElement('div'); |
| 67 | + bubble.classList.add('bubble'); |
| 68 | + customDiv.appendChild(bubble); |
| 69 | + const pointer = document.createElement('div'); |
| 70 | + pointer.classList.add('pointer'); |
| 71 | + customDiv.appendChild(pointer); |
| 72 | + |
| 73 | + // Define a method to set the content of custom label |
| 74 | + function setLabelContent(properties) { |
| 75 | + const coord = properties.position.as('EPSG:4326'); |
| 76 | + bubble.textContent = ` |
| 77 | + lon : ${Math.round(coord.x * 100) / 100}° | |
| 78 | + lat : ${Math.round(coord.y * 100) / 100}° | |
| 79 | + alt : ${Math.round(coord.z)}m `; |
| 80 | + return customDiv; |
| 81 | + } |
| 82 | + |
| 83 | + // Add a Label on `p` + click |
| 84 | + let pickMode = false; |
| 85 | + viewerDiv.addEventListener('keydown', function () { |
| 86 | + if (event.key === 'p') { pickMode = true; } |
| 87 | + }, false); |
| 88 | + viewerDiv.addEventListener('keyup', function () { |
| 89 | + if (event.key === 'p') { pickMode = false; } |
| 90 | + }, false); |
| 91 | + viewerDiv.addEventListener('mousedown', addFeatureFromMouseEvent, false); |
| 92 | + |
| 93 | + // this const shall receive the mouse coordinates when user clicks while holding `p` pressed |
| 94 | + const featureCoord = new itowns.Coordinates(view.referenceCrs); |
| 95 | + |
| 96 | + function addFeatureFromMouseEvent(event) { |
| 97 | + if (!pickMode) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + featureCoord.setFromVector3(view.getPickingPositionFromDepth(view.eventToViewCoords(event))); |
| 102 | + |
| 103 | + const features = createFeatureAt(featureCoord); |
| 104 | + |
| 105 | + // the source of the feature layer |
| 106 | + const source = new itowns.FileSource({ features }); |
| 107 | + |
| 108 | + // create labelLayer |
| 109 | + const layer = new itowns.LabelLayer('idLabelLayer', { |
| 110 | + source: source, |
| 111 | + domElement: setLabelContent, |
| 112 | + style: new itowns.Style({ |
| 113 | + text: { anchor: [-0.8, -1] }, |
| 114 | + }), |
| 115 | + }); |
| 116 | + |
| 117 | + view.addLayer(layer); |
| 118 | + |
| 119 | + // remove mouse event listener to prevent adding other layer sharing `idLabelLayer` id |
| 120 | + viewerDiv.removeEventListener('mousedown', addFeatureFromMouseEvent, false); |
| 121 | + } |
| 122 | + |
| 123 | + function createFeatureAt(coordinate) { |
| 124 | + // create new featureCollection |
| 125 | + const collection = new itowns.FeatureCollection({ |
| 126 | + crs: view.tileLayer.extent.crs, |
| 127 | + buildExtent: true, |
| 128 | + structure: '2d', |
| 129 | + }); |
| 130 | + |
| 131 | + // create new feature |
| 132 | + const feature = collection.requestFeatureByType(itowns.FEATURE_TYPES.POINT); |
| 133 | + |
| 134 | + // add geometries to feature |
| 135 | + const geometry = feature.bindNewGeometry(); |
| 136 | + geometry.startSubGeometry(1, feature); |
| 137 | + geometry.pushCoordinates(coordinate, feature); |
| 138 | + geometry.properties.position = coordinate; |
| 139 | + |
| 140 | + geometry.updateExtent(); |
| 141 | + feature.updateExtent(geometry); |
| 142 | + collection.updateExtent(feature.extent); |
| 143 | + |
| 144 | + return collection; |
| 145 | + } |
| 146 | +</script> |
| 147 | +</body> |
| 148 | +</html> |
0 commit comments