|
1 | | -// See also https://openlayers.org/en/latest/examples/tooltip-on-hover.html |
| 1 | +/* |
| 2 | +taken from https://openlayers.org/en/latest/examples/tooltip-on-hover.html |
| 3 | +*/ |
| 4 | +import { Map } from "ol"; |
| 5 | +import type { FeatureLike } from "ol/Feature"; |
| 6 | +import type { Pixel } from "ol/pixel"; |
2 | 7 |
|
3 | | -import { type Map } from "ol"; |
4 | | -import { type FeatureLike } from "ol/Feature"; |
5 | | -import Overlay from "ol/Overlay"; |
| 8 | +import mustache from "mustache"; |
6 | 9 |
|
7 | | -type FeatureProps = { |
8 | | - [x: string]: any; |
9 | | -} |
| 10 | +import { getFeatureProperties } from "./utils"; |
10 | 11 |
|
11 | | -function createElement(cssText?: string | undefined): HTMLElement { |
12 | | - const el = document.createElement("div"); |
13 | | - el.id = "ol-tooltip"; |
14 | | - el.style.cssText = cssText || "padding: 5px; background-color: #333; color: #fff; border-radius: 4px; z-index: 100;" |
15 | | - el.style.visibility = "hidden"; |
16 | | - return el; |
| 12 | +/* |
| 13 | +#info { |
| 14 | + position: absolute; |
| 15 | + display: inline-block; |
| 16 | + height: auto; |
| 17 | + width: auto; |
| 18 | + z-index: 100; |
| 19 | + background-color: #333; |
| 20 | + color: #fff; |
| 21 | + text-align: center; |
| 22 | + border-radius: 4px; |
| 23 | + padding: 5px; |
| 24 | + left: 50%; |
| 25 | + transform: translateX(3%); |
| 26 | + visibility: hidden; |
| 27 | + pointer-events: none; |
17 | 28 | } |
| 29 | +*/ |
| 30 | + |
| 31 | +const info = document.createElement("div"); |
| 32 | +info.style.position = "absolute"; |
| 33 | +info.style.display = "inline-block"; |
| 34 | +info.style.height = "auto"; |
| 35 | +info.style.width = "auto"; |
| 36 | +info.style.zIndex = "100"; |
| 37 | +info.style.backgroundColor = "#333"; |
| 38 | +info.style.color = "#fff"; |
| 39 | +// info.style.textAlign = "center"; |
| 40 | +info.style.borderRadius = "4px"; |
| 41 | +info.style.padding = "7px"; |
| 42 | +info.style.left = "50%"; |
| 43 | +// info.style.transform = "translateX(3%)"; |
| 44 | +info.style.visibility = "hidden"; |
| 45 | +info.style.pointerEvents = "none"; |
| 46 | + |
| 47 | +function renderFeatureProperties(feature: FeatureLike, template: string | null): string { |
| 48 | + const properties = getFeatureProperties(feature); |
| 49 | + if (template) |
| 50 | + return mustache.render(template, properties); |
18 | 51 |
|
19 | | -function getFeatureProperties(feature: FeatureLike): FeatureProps { |
20 | | - let { geometry, ...props } = feature.getProperties(); |
21 | | - return props; |
| 52 | + return Object.keys(properties).map((key) => `${key}: ${properties[key]}`).join("</br>"); |
22 | 53 | } |
23 | 54 |
|
24 | | -function addTooltipTo(map: Map, prop: string): void { |
25 | | - let el = createElement(); |
26 | | - const overlay = new Overlay({ element: el }); |
27 | | - map.addOverlay(overlay); |
| 55 | +function addTooltipToMap(map: Map, template: string | null): void { |
| 56 | + info.id = "ol-tooltip"; |
| 57 | + map.getTargetElement().appendChild(info); |
| 58 | + console.log("tooltip element added", info); |
| 59 | + |
28 | 60 | let currentFeature: FeatureLike | undefined; |
29 | | - map.on('pointermove', (e) => { |
30 | | - if (e.dragging) |
31 | | - return; |
32 | | - const feature = map.forEachFeatureAtPixel(e.pixel, (feature) => { |
33 | | - return feature; |
34 | | - }); |
| 61 | + const displayFeatureInfo = function (pixel: Pixel, target: EventTarget | null) { |
| 62 | + // @ts-expect-error |
| 63 | + const feature = target.closest('.ol-control') |
| 64 | + ? undefined |
| 65 | + : map.forEachFeatureAtPixel(pixel, function (feature) { |
| 66 | + return feature; |
| 67 | + }); |
35 | 68 | if (feature) { |
36 | | - el.style.visibility = "visible"; |
37 | | - overlay.setPosition(e.coordinate); |
| 69 | + // console.log("feature props", getFeatureProperties(feature)); |
| 70 | + info.style.left = (pixel[0] + 15) + 'px'; |
| 71 | + info.style.top = pixel[1] + 'px'; |
38 | 72 | if (feature !== currentFeature) { |
39 | | - console.log("feature props", getFeatureProperties(feature)); |
40 | | - el.innerHTML = feature.get(prop)?.toString() || ""; |
| 73 | + info.style.visibility = 'visible'; |
| 74 | + info.innerHTML = renderFeatureProperties(feature, template); |
41 | 75 | } |
42 | 76 | } else { |
43 | | - el.style.visibility = "hidden"; |
| 77 | + info.style.visibility = 'hidden'; |
44 | 78 | } |
45 | 79 | currentFeature = feature; |
| 80 | + }; |
| 81 | + |
| 82 | + map.on('pointermove', function (evt) { |
| 83 | + if (evt.dragging) { |
| 84 | + info.style.visibility = 'hidden'; |
| 85 | + currentFeature = undefined; |
| 86 | + return; |
| 87 | + } |
| 88 | + displayFeatureInfo(evt.pixel, evt.originalEvent.target); |
| 89 | + }); |
| 90 | + |
| 91 | + map.on('click', function (evt) { |
| 92 | + displayFeatureInfo(evt.pixel, evt.originalEvent.target); |
46 | 93 | }); |
47 | 94 |
|
48 | | - map.getTargetElement().addEventListener("pointerleave", () => { |
49 | | - el.style.visibility = "hidden"; |
| 95 | + map.getTargetElement().addEventListener('pointerleave', function () { |
50 | 96 | currentFeature = undefined; |
| 97 | + info.style.visibility = 'hidden'; |
51 | 98 | }); |
52 | 99 | } |
53 | 100 |
|
54 | | -export { addTooltipTo } |
| 101 | +export { addTooltipToMap }; |
0 commit comments