Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 44x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 26x 18x 8x 8x 8x 18x 18x 18x 18x 18x 18x 18x 26x 26x 18x 18x 2x 18x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 18x 74004x 21x 19x 19x 19x 19x 19x 2x 2x 21x 21x 21x 21x 21x 21x 21x 21x 21x 20x 2x 2x 20x 20x 20x 20x 40x 40x 2x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 2x 2x 4x 42x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 2x 2x 3x 1x 1x 1x 1x 1x 3x 2x 2x 2x 1x 25x 3x 25x 25x 81054x 81054x 81054x 25x 25x 25x 24x 24x 3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 6x 1x 1x 5x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 6x 1x 1x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 3x 3x 81058x 81058x 81057x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* Copyright (c) Siemens 2016 - 2026
* SPDX-License-Identifier: MIT
*/
import {
AfterViewChecked,
AfterViewInit,
ChangeDetectionStrategy,
Component,
computed,
contentChild,
ElementRef,
HostListener,
inject,
Injector,
input,
model,
NgZone,
OnChanges,
OnDestroy,
output,
SimpleChanges,
Type,
viewChild
} from '@angular/core';
import {
SiNoTranslateService,
SiTranslateServiceBuilder,
t
} from '@siemens/element-translate-ng/translate';
import { apply as applyMapboxStyle } from 'ol-mapbox-style';
import Control from 'ol/control/Control';
import { defaults as defaultControls } from 'ol/control/defaults';
import { Coordinate } from 'ol/coordinate';
import * as olExtent from 'ol/extent';
import Feature, { FeatureLike } from 'ol/Feature';
import GeoJSON from 'ol/format/GeoJSON';
import Geometry from 'ol/geom/Geometry';
import Point from 'ol/geom/Point';
import Layer from 'ol/layer/Layer';
import VectorLayer from 'ol/layer/Vector';
import Map from 'ol/Map';
import MapBrowserEvent from 'ol/MapBrowserEvent';
import Overlay, { Positioning } from 'ol/Overlay';
import RenderFeature from 'ol/render/Feature';
import Cluster from 'ol/source/Cluster';
import VectorSource from 'ol/source/Vector';
import { StyleLike } from 'ol/style/Style';
import View from 'ol/View';
import { SiMapPopoverComponent } from './components/si-map-popover/si-map-popover.component';
import { SiMapTooltipComponent } from './components/si-map-tooltip/si-map-tooltip.component';
import { ColorPalette } from './models/color-palette.type';
import {
ANIMATION_DURATION,
DEFAULT_CLUSTER_CLICK_ZOOM,
DEFAULT_CLUSTER_DISTANCE,
DEFAULT_FEATURE_CLICK_ZOOM,
DEFAULT_FEATURE_SELECT_ZOOM,
DEFAULT_GLOBAL_ZOOM,
GEOJSON_LAYER,
LAYER_NAME,
POINTS_LAYER
} from './models/constants';
import { MapPoint, MapPointMetaData } from './models/map-point.interface';
import { OverlayNativeProperties } from './models/overlay-native-properties.interface';
import { MapService } from './services/map.service';
import { SelectCluster } from './services/SelectCluster';
import { styleJson } from './shared/themes/style';
interface ClusterHolder {
feature?: Feature;
}
@Component({
selector: 'si-map',
imports: [SiMapTooltipComponent, SiMapPopoverComponent],
templateUrl: './si-map.component.html',
styleUrl: './si-map.component.scss',
providers: [MapService],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiMapComponent implements AfterViewInit, OnChanges, OnDestroy, AfterViewChecked {
/**
* Points to be rendered on the map as features.
*/
readonly points = model<MapPoint[]>();
/**
* Payload of a geo-JSON object.
*/
readonly geoJson = input<any>();
/**
* Callback function to add custom styling to the drawn geo-JSON features.
*/
readonly styleFunction = input<StyleLike>();
/**
* Projection format used to render the geo-JSON payload.
*
* @defaultValue 'EPSG:3857'
*/
readonly geoJsonProjection = input('EPSG:3857');
/**
* Enable or disable animation of markers.
*
* @defaultValue false
*/
readonly markerAnimation = input(false);
/**
* Enable or disable clustering of points on the map.
*
* @defaultValue false
*/
readonly cluster = input(false);
/**
* Defines which colors should be used when points are grouped. Grouping means
* that points are painted with their group color and they are displayed as
* series in donut chart inside cluster.
*
* @defaultValue 'status'
*/
readonly groupColors = input<Record<number, string> | ColorPalette>('status');
/**
* - **overlay**: Layer for popup element, an element to be displayed over the
* map and attached to a single map location.
* - **controls**: List of controls to activate for map, additional controls
* like full screen, etc. can be added here.
*/
readonly nativeProperties = input<OverlayNativeProperties>();
/**
* Type of background map, for example `'https://api.maptiler.com/maps/voyager/style.json?key=xxx'`
* (Token key is required). The styleJson defines the tiler source and the
* used styles for different layers, should only be used if the siemens
* default styles by using `maptilerKey` isn't good enough.
*/
readonly styleJson = input<any>();
/**
* Option to switch on/off the display of tooltip while hovering over single
* point.
*
* @defaultValue false
*/
readonly displayTooltipOnHover = input(false);
/**
* Option to switch on/off the display of popover when clicked.
* Default set to true (on).
*
* @defaultValue true
*/
readonly displayPopoverOnClick = input(true);
/**
* Option to switch on/off popover when hovered.
* Default is set to false (off)
*
* @defaultValue false
*/
readonly displayPopoverOnHover = input(false);
/**
* Provide your key for MapTiler, this will use the default siemens element
* styles which can be used in both light and dark mode. Shouldn't be used
* together with styleJson.
*/
readonly maptilerKey = input<string>();
/**
* Option to switch on/off the display of tooltip while hovering over cluster.
*
* @defaultValue false
*/
readonly displayTooltipOnClusterHover = input(false);
/**
* Defines range in which points are clustered. In pixels.
*
* @defaultValue DEFAULT_CLUSTER_DISTANCE
*/
readonly clusterDistance = input<number>(DEFAULT_CLUSTER_DISTANCE);
/**
* Zoom level when calling the `select()` method on the component.
* Defaults to 10.
*
* @defaultValue DEFAULT_FEATURE_SELECT_ZOOM
*/
readonly featureSelectZoom = input<number>(DEFAULT_FEATURE_SELECT_ZOOM);
/**
* Max zoom level which is used when clicking on feature on the map.
* Defaults to 15.
*
* @defaultValue DEFAULT_FEATURE_CLICK_ZOOM
*/
readonly featureClickZoom = input<number>(DEFAULT_FEATURE_CLICK_ZOOM);
/**
* Max zoom level for clicking on a cluster on the map. Defaults to 15.
*
* @defaultValue DEFAULT_CLUSTER_CLICK_ZOOM
*/
readonly clusterClickZoom = input<number>(DEFAULT_CLUSTER_CLICK_ZOOM);
/**
* Max zoom level on load or when clicking the reset button. Defaults to 5.
*
* @defaultValue DEFAULT_GLOBAL_ZOOM
*/
readonly globalZoom = input<number>(DEFAULT_GLOBAL_ZOOM);
/**
* When enabled, features will be automatically zoom to on click.
*
* @defaultValue false
*/
readonly autoZoomFeatureClick = input(false);
/**
* When disabled, clusters will no longer automatically be zoomed to on click.
*
* @defaultValue true
*/
readonly autoZoomClusterClick = input(true);
/**
* Custom popover component
*/
readonly popoverComponent = input<Type<any>>();
/**
* Custom popover component for clustering
*/
readonly clusterPopoverComponent = input<Type<any>>();
/**
* Custom popover component
*/
readonly popoverComponentProps = input<any>();
/**
* Close the popover on click
*
* @defaultValue true
*/
readonly popoverCloseOnClick = input(true);
/**
* Padding (in pixels) applied to the map view when zooming to a cluster, preventing points from being clipped at the edges.
* Specified as [top, right, bottom, left].
*
* @defaultValue [20, 20, 20, 20]
*/
readonly fitClusterPadding = input<[number, number, number, number]>([20, 20, 20, 20]);
/**
* For Point geometry, padding (in pixels) to be cleared to fit the point inside the view after a click on it.
* Values in the array are: [top, right, bottom, left].
*
* @defaultValue [20, 20, 20, 20]
*/
readonly fitPointPadding = input<number[]>([20, 20, 20, 20]);
/**
* For GeoJson, padding (in pixels) to be cleared to fit the GeoJson inside the view after a click on it.
* Values in the array are: [top, right, bottom, left].
*
* @defaultValue [20, 20, 20, 20]
*/
readonly fitGeoJsonPadding = input<number[]>([20, 20, 20, 20]);
/**
* Cutoff text for tooltips, when cluster combines more than 4 features
*
* @deprecated Use `SiMapTooltipComponent` instead to set the `moreText` input e.g. `<si-map ...><si-map-tooltip [moreText]="'KEY_MORE'" /></si-map>`.
*
* @defaultValue
* ```
* t(() => $localize`:@@SI_MAPS.TOOLTIP_MORE_TEXT:and {{length}} more...`)
* ```
*/
readonly moreText = input(
t(() => $localize`:@@SI_MAPS.TOOLTIP_MORE_TEXT:and {{length}} more...`)
);
/**
* Show multiple worlds, including points that cross the 180th meridian.
*
* @defaultValue true
*/
readonly multiWorld = input(true);
/**
* Show the label permanently next to the point.
* Normally the label is shown on hover or click.
* By enabling this it will always be visible next to the marker.
*
* @defaultValue false
*/
readonly alwaysShowLabels = input(false);
/**
* Maximum number of characters per line for always displayed labels of a point.
* If the label exceeds this limit, it will be wrapped or trimmed.
* If set to 0, label trimming will be disabled.
* This setting is effective only when {@link alwaysShowLabels} is enabled.
* Important: Changing this is not recommended in most cases and may cause issues with positioning of the labels.
*
* @defaultValue 20
*/
readonly maxLabelLineLength = input(20);
/**
* Maximum number of lines for always displayed labels of a point.
* If the label exceeds this limit, it will be trimmed.
* This setting is effective only when {@link alwaysShowLabels} is enabled and {@link maxLabelLineLength} is not 0.
* Important: Changing this is not recommended in most cases and may cause issues with positioning of the labels.
*
* @defaultValue 3
*/
readonly maxLabelLines = input(3);
/**
* Emitted when points on the map are selected or de-selected
*/
readonly pointsSelected = output<MapPoint[]>();
/**
* Emitted when the points are available as features. So consumers can call methods like `zoomToPoints()` on the actual point list.
*/
readonly pointsRefreshed = output<void>();
protected readonly popover = viewChild.required(SiMapPopoverComponent);
protected readonly popoverElement = viewChild.required<ElementRef, ElementRef>('popover', {
read: ElementRef
});
protected readonly defaultTooltip = viewChild(SiMapTooltipComponent);
protected readonly projectedTooltip = contentChild(SiMapTooltipComponent);
protected readonly tooltip = computed(() => this.projectedTooltip() ?? this.defaultTooltip()!);
protected readonly mapReference = viewChild.required<ElementRef<HTMLElement>>('map');
/** OpenLayers map instance. */
map?: Map;
tooltipOverlay!: Overlay;
popoverOverlay!: Overlay;
/** @defaultValue false */
isClicked = false;
/** @defaultValue [] */
features: Feature[] = [];
selected?: Feature;
targetFeature?: Feature;
protected attributionsLabel = t(() => $localize`:@@SI_MAPS.ATTRIBUTIONS_BUTTON:Attributions`);
protected zoomInLabel = t(() => $localize`:@@SI_MAPS.ZOOM_IN_BUTTON:Zoom in`);
protected zoomOutLabel = t(() => $localize`:@@SI_MAPS.ZOOM_OUT_BUTTON:Zoom out`);
protected zoomToDefaultLabel = t(
() => $localize`:@@SI_MAPS.ZOOM_TO_DEFAULT_BUTTON:Zoom to default view`
);
protected clusterInteraction?: SelectCluster;
private selectedCluster?: Feature;
private darkTheme: boolean | undefined = undefined;
private readonly mapService = inject(MapService);
private readonly ngZone = inject(NgZone);
// We cannot use new inject function, as it may not be available in older translate version.
// Older version could potentially be used in mobile.
private readonly translateService =
inject(SiTranslateServiceBuilder, { optional: true })?.buildService(inject(Injector)) ??
inject(SiNoTranslateService);
constructor() {
const translations = this.translateService.translateSync([
this.attributionsLabel,
this.zoomInLabel,
this.zoomOutLabel,
this.zoomToDefaultLabel
]);
this.attributionsLabel = translations[this.attributionsLabel];
this.zoomInLabel = translations[this.zoomInLabel];
this.zoomOutLabel = translations[this.zoomOutLabel];
this.zoomToDefaultLabel = translations[this.zoomToDefaultLabel];
}
// this is to filter out non-data layers for performance of `forEachFeatureAtPixel()`
private layerFilter = (layer: Layer<any>): boolean => {
const name = layer.get(LAYER_NAME);
return name === POINTS_LAYER || name === GEOJSON_LAYER;
};
ngOnChanges(changes: SimpleChanges<this>): void {
if (!this.map) {
return;
}
Iif (changes.groupColors) {
this.mapService.setTheme(this.groupColors());
// refresh the style of points-layer to reflect the color changes.
this.refreshLayersStyle([POINTS_LAYER]);
}
Iif (changes.points) {
this.refresh(this.points() ?? [], false);
}
Iif (changes.geoJson || changes.styleFunction) {
const geojsonLayer = this.map
.getLayers()
.getArray()
.find(layer => layer.get(LAYER_NAME) === GEOJSON_LAYER) as VectorLayer<VectorSource>;
if (geojsonLayer) {
if (changes.geoJson) {
// If geojson-layer is already present and there is change in geojson,
// update the features of geojson-layer with the latest geojson.
const geoJson = this.geoJson();
const features =
geoJson && Object.keys(geoJson).length
? new GeoJSON({ featureProjection: this.geoJsonProjection() }).readFeatures(geoJson)
: [];
this.ngZone.runOutsideAngular(() =>
geojsonLayer.setSource(new VectorSource({ features }))
);
}
if (changes.styleFunction) {
// If there is some change in styleFunction, update the style of geojson-layer.
this.ngZone.runOutsideAngular(() => geojsonLayer.setStyle(this.styleFunction()));
}
} else {
// If geojson-layer is not present, and change is detected in geojson or styleFunction,
// create a new geojson-layer.
this.ngZone.runOutsideAngular(() => this.setGeoJsonLayer());
}
}
}
ngAfterViewInit(): void {
this.ngZone.runOutsideAngular(() => {
this.init();
this.map?.on('pointermove', event => this.onFeatureHover(event));
this.map?.on('click', event => this.onClusterClick(event));
});
const dark = document.documentElement.classList.contains('app--dark');
this.setTheme(dark, this.maptilerKey());
setTimeout(() => this.fixCenter());
}
ngAfterViewChecked(): void {
// making sure that if the map is not rendered updateSize() gets called
// this is to make sure the map also gets rendered in ionic
Eif (this.map && !this.map.isRendered()) {
this.ngZone.runOutsideAngular(() => this.map!.updateSize());
}
}
ngOnDestroy(): void {
this.removePixelRatioListener();
this.map?.setTarget(undefined);
}
@HostListener('window:theme-switch', ['$event'])
onThemeSwitchInternal(event: Event): void {
this.setTheme((event as CustomEvent).detail.dark, this.maptilerKey());
}
onThemeSwitch(dark: boolean): void {
this.setTheme(dark, this.maptilerKey());
}
private removePixelRatioListener = (): void => {};
private readonly updatePixelRatio = (): void => {
// on browser zoom the pixel ratio changes. No other updates are needed as OL has a
Iif (this.map && window.devicePixelRatio !== (this.map as any).pixelRatio_) {
this.ngZone.runOutsideAngular(() => this.changeOpenLayerPixelRation());
}
this.removePixelRatioListener();
const media = matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
media.addEventListener('change', this.updatePixelRatio);
this.removePixelRatioListener = () =>
media.removeEventListener('change', this.updatePixelRatio);
};
/**
* Changes the pixel ration in OL which is not supported directly by OL.
*/
private changeOpenLayerPixelRation(): void {
if (!this.map) {
return;
}
// need to reset cached styles as they contain pre-rendered canvases
this.mapService.resetCaches();
// set private property
(this.map as any).pixelRatio_ = devicePixelRatio;
// force redraw of all layers
this.map!.getLayers().forEach(layer => {
if (layer instanceof Layer) {
const source = layer.getSource();
if (source?.tilePixelRatio_ !== undefined) {
source.tilePixelRatio_ = devicePixelRatio;
source.refresh();
}
if (source instanceof Cluster) {
source.getSource()?.changed();
} else {
source?.changed();
}
layer.changed();
}
});
this.map.updateSize();
}
/**
* Initialize map and it's layers
*/
init(): void {
const tooltipContainer = this.tooltip().nativeElement;
const popoverContainer = this.popoverElement().nativeElement;
const mapContainer = this.mapReference().nativeElement;
const initialView = {
constrainResolution: true,
showFullExtent: true,
center: [0, 0],
zoom: 2,
minZoom: 2,
maxZoom: 24
};
this.tooltipOverlay = this.createOverlay(
tooltipContainer,
'top-center',
this.nativeProperties(),
true
);
this.popoverOverlay = this.createOverlay(
popoverContainer,
'top-center',
this.nativeProperties()
);
const controls = defaultControls({
attributionOptions: {
label: '',
tipLabel: this.attributionsLabel,
expandClassName: 'element-state-info',
collapseLabel: '',
collapseClassName: 'element-left-2 flip-rtl'
},
zoomOptions: {
zoomInLabel: '',
zoomInTipLabel: this.zoomInLabel,
zoomInClassName: 'element-plus',
zoomOutLabel: '',
zoomOutTipLabel: this.zoomOutLabel,
zoomOutClassName: 'element-minus'
},
rotateOptions: {
label: '',
compassClassName: 'element-compass-needle'
}
});
class ResetZoomControl extends Control {
constructor(options: { label: string; callback: () => void }) {
const button = document.createElement('button');
button.className = 'element-zoom-reset';
button.title = options.label;
const element = document.createElement('div');
element.className = 'ol-zoom-extent ol-unselectable ol-control';
element.appendChild(button);
super({ element: element });
button.addEventListener('click', options.callback, false);
}
}
controls.push(
new ResetZoomControl({
label: this.zoomToDefaultLabel,
callback: () => {
if (this.features.length) {
this.zoomToFeatures(this.features, this.globalZoom());
} else {
// reset to original view
this.map?.setView(new View(initialView));
}
}
})
);
const nativeProperties = this.nativeProperties();
Iif (nativeProperties?.controls) {
nativeProperties.controls.forEach(e => controls.push(e));
}
this.map = new Map({
controls,
target: mapContainer,
overlays: [this.tooltipOverlay, this.popoverOverlay],
view: new View(initialView)
});
this.mapService.setZoomCallback(() => this.map?.getView().getZoom());
this.mapService.setTheme(this.groupColors()); // Set theme before adding layers.
const styleJsonValue = this.styleJson();
Iif (styleJsonValue) {
this.setMapStyle(undefined, undefined, styleJsonValue);
}
this.setGeoJsonLayer();
const points = this.points();
Eif (points?.length) {
this.features = this.mapService.createFeatures(points, this.alwaysShowLabels());
}
this.setLayers();
this.zoomToFeatures(this.features, this.globalZoom(), false);
this.updatePixelRatio();
}
private centerValid(): boolean {
const center = this.map?.getView()?.getCenter();
return !!center && !isNaN(center[0]);
}
// workaround for center not being applied sometimes
private fixCenter(): void {
Iif (!this.centerValid()) {
this.ngZone.runOutsideAngular(() => this.map?.getView()?.setCenter([0, 0]));
}
}
/**
* Setting up the layers for the map (cluster/vector layer)
*/
private setLayers(): void {
let layer: Layer;
let labelLayer: Layer | undefined;
const showLabels = this.features.some(feature => feature.getProperties().label?.text);
if (this.cluster()) {
const clusterLayer = this.mapService.clusterLayer(
this.features,
this.markerAnimation(),
this.clusterDistance(),
this.multiWorld()
);
this.clusterInteraction = new SelectCluster({
maxZoom: this.featureClickZoom,
zoomOnClick: this.autoZoomClusterClick,
fitClusterPadding: this.fitClusterPadding,
// Style the selection
style: (feature: FeatureLike) => this.mapService.getClusterStyle(feature as Feature)
});
this.map?.addInteraction(this.clusterInteraction);
Iif (showLabels) {
labelLayer = this.mapService.labelLayer(
this.features,
this.multiWorld(),
this.maxLabelLineLength(),
this.maxLabelLines(),
clusterLayer
);
}
layer = clusterLayer;
} else {
layer = this.mapService.vectorLayer(this.features, this.multiWorld());
Iif (showLabels) {
labelLayer = this.mapService.labelLayer(
this.features,
this.multiWorld(),
this.maxLabelLineLength(),
this.maxLabelLines()
);
}
}
layer.set(LAYER_NAME, POINTS_LAYER);
this.map?.addLayer(layer);
Iif (labelLayer) {
this.map?.addLayer(labelLayer);
}
}
/**
* Setting up the geoJson layer
*/
setGeoJsonLayer(): void {
const geoJson = this.geoJson();
Eif (this.map && geoJson && Object.keys(geoJson).length > 0) {
const layer = this.mapService.vectorLayer(
new GeoJSON({ featureProjection: this.geoJsonProjection() }).readFeatures(geoJson),
this.multiWorld()
);
layer.setStyle(this.styleFunction());
layer.set(LAYER_NAME, GEOJSON_LAYER);
this.ngZone.runOutsideAngular(() => this.map!.addLayer(layer));
}
}
private setTheme(dark: boolean, maptilerKey?: string): void {
if (maptilerKey && (this.darkTheme === undefined || this.darkTheme !== dark)) {
this.setMapStyle(dark, maptilerKey);
this.darkTheme = dark;
}
this.mapService.setTheme(this.groupColors());
// reload the style to reflect the theme changes.
this.refreshLayersStyle([POINTS_LAYER, GEOJSON_LAYER]);
}
private refreshLayersStyle(layerNames: string[]): void {
this.ngZone.runOutsideAngular(() =>
this.map
?.getLayers()
.getArray()
.forEach(layer => {
Eif (layerNames.includes(layer.get(LAYER_NAME))) {
(layer as VectorLayer<VectorSource>).setStyle(
(layer as VectorLayer<VectorSource>).getStyle()
);
}
})
);
}
/**
* Set Map style
* @param dark - boolean if dark theme should be active or not
* @param key - the api key for the maptiler styleJSON
* @param styleJSON - a preexisting styleJSON as url with key already provided
*/
setMapStyle(dark?: boolean, key?: string, styleJSON?: string): void {
Iif (!this.map) {
return;
}
Iif (styleJSON) {
this.ngZone.runOutsideAngular(() => applyMapboxStyle(this.map!, styleJSON));
E} else if (key) {
const layers = this.map.getLayers().getArray() as Layer[];
layers.forEach((layer: Layer) => {
Iif (layer.get('mapbox-source')) {
this.map!.removeLayer(layer);
}
});
this.mapboxStyles(styleJson(key, dark));
}
}
/**
* Set mapbox styles
* @param styleJSON - the styleJSON as a JSON object
*/
mapboxStyles(styleJSON: any): void {
Iif (!this.map) {
return;
}
this.ngZone.runOutsideAngular(() =>
applyMapboxStyle(this.map!, styleJSON).then(mapOrLayer => {
const map = mapOrLayer as Map;
const layers = map.getLayers().getArray() as Layer[];
layers.forEach((layer: Layer) => {
Iif (layer.get('mapbox-source')) {
// any needs to be used here otherwise library doesn't allow it
(layer.getSource() as any).wrapX_ = this.multiWorld();
this.map = map;
}
});
})
);
}
/**
* Create overlay for tooltip and popover
* @param nativeProperties - native properties
* @param container - to create overlay in
* @param position - overlay positioning
*/
createOverlay(
container: HTMLElement,
position: Positioning,
nativeProperties?: OverlayNativeProperties,
isTooltip?: boolean
): Overlay {
return (
nativeProperties?.overlay ??
new Overlay({
element: container,
positioning: position,
// Stop event bubbling which allow to scroll in the popover
stopEvent: true,
autoPan: isTooltip ? true : false
})
);
}
/**
* On feature hovering show tooltip and change cursor style
* @param event - A hovering event
*/
onFeatureHover(event: MapBrowserEvent): void {
Iif (!this.map) {
return;
}
const clusterHolder: ClusterHolder = {};
const features = this.map.forEachFeatureAtPixel(
event.pixel,
feat => this.featureCallback(feat, clusterHolder),
{
hitTolerance: 2,
layerFilter: this.layerFilter
}
);
const element = this.map.getTargetElement();
Eif (element) {
// The cursor will change to pointer if displayPopoverOnClick is true or if Cluster Features are hovered.
element.style.cursor =
features?.length && (this.displayPopoverOnClick() || features.length > 1) ? 'pointer' : '';
// The popover will be shown only for individual points, not for cluster.
Iif (this.displayPopoverOnHover() && !this.isClicked) {
if (features?.length === 1) {
this.setPopOver(features[0], false, event);
} else {
this.popoverOverlay.setPosition(undefined);
}
return;
}
}
this.doShowTooltip(features, clusterHolder.feature);
}
/** Display tooltip on hovering over feature type of Feature. */
displayTooltip(feats: FeatureLike[]): void {
Iif (!this.displayTooltipOnHover() || this.isClicked) {
return;
}
for (const feat of feats) {
const features = this.featureCallback(feat);
if (features?.length === 1) {
this.showTooltipContent(features[0]);
return;
}
}
}
private doShowTooltip(features?: Feature[], cluster?: Feature): void {
let shown = false;
Iif (!this.isClicked && features?.length) {
if (this.displayTooltipOnHover() && features?.length === 1) {
shown = this.showTooltipContent(features[0]);
} else if (this.selectedCluster !== cluster && this.displayTooltipOnClusterHover()) {
shown = this.showClusterTooltipContent(features, cluster);
}
}
Eif (!shown) {
this.tooltipOverlay.setPosition(undefined);
}
}
/**
* Clicking on cluster event
* @param event - openlayers event
*/
onClusterClick(event: MapBrowserEvent): void {
Iif (!this.map) {
return;
}
Iif (!this.popoverCloseOnClick() && this.popoverElement()) {
const popoverLoc = (
this.popoverElement().nativeElement as HTMLElement
).firstElementChild!.getBoundingClientRect();
const mapLoc = (this.mapReference().nativeElement as HTMLElement).getBoundingClientRect();
const left = popoverLoc.left - mapLoc.left;
const top = popoverLoc.top - mapLoc.top;
const right = popoverLoc.right - mapLoc.left;
const bottom = popoverLoc.bottom - mapLoc.top;
if (
left <= event.pixel[0] &&
event.pixel[0] <= right &&
top <= event.pixel[1] &&
event.pixel[1] <= bottom
) {
return;
}
}
const clusterHolder: ClusterHolder = {};
const clusterFeatures = this.map.forEachFeatureAtPixel(
event.pixel,
feat => this.featureCallback(feat, clusterHolder),
{
layerFilter: this.layerFilter
}
);
Iif (clusterFeatures) {
if (clusterFeatures.length > 1) {
this.tooltipOverlay.setPosition(undefined);
this.showClusterPopover(clusterHolder.feature!);
this.emitSelection(clusterFeatures);
} else if (clusterFeatures.length === 1) {
if (clusterFeatures[0] instanceof RenderFeature) {
this.closePopup();
this.emitSelection();
} else if (this.displayPopoverOnClick()) {
this.featureClick(clusterFeatures[0], event, this.autoZoomFeatureClick());
this.emitSelection(clusterFeatures);
}
}
} else {
this.closePopup();
this.emitSelection();
}
}
private emitSelection(features?: Feature[]): void {
const points: MapPoint[] = (features ?? []).map(f => {
const props = f.getProperties();
return props.meta ?? (props as MapPoint);
});
this.ngZone.run(() => this.pointsSelected.emit(points));
}
/**
* Zoom to cluster features
* @param clusterFeatures - features to zoom in
*/
zoomCluster(clusterFeatures: Feature[]): void {
this.clusterInteraction?.zoomToCluster(clusterFeatures);
}
/**
* Close popup
*/
closePopup(): boolean {
this.selectedCluster = undefined;
this.popoverOverlay.setPosition(undefined);
this.isClicked = false;
return false;
}
/**
* Return features inside feature object in case of cluster enabled/disabled
* @param feat - feature to get features from
*/
featureCallback(feat: FeatureLike, clusterHolder?: ClusterHolder): Feature[] | undefined {
if (feat instanceof Feature) {
const propFeatures = feat.getProperties().features;
Iif (this.cluster() && propFeatures) {
if (clusterHolder) {
clusterHolder.feature = feat;
}
return propFeatures;
}
return [feat];
}
return undefined;
}
/**
* Zoom to features, gets coordinates of points and creates bounding extent
* @param points - list of points to zoom in, if empty all points will be zoomed to, need to be the same reference as the input
* @param zoomLevel - number for level to zoom in
*/
zoomToPoints(points: MapPoint[], zoomLevel: number, shouldAnimate = true): void {
if (!points.length) {
this.zoomToFeatures([], zoomLevel, shouldAnimate);
} else {
const features = points
.map(point => this.points()?.indexOf(point))
.filter(index => index !== undefined)
.map(index => this.features[index]);
this.zoomToFeatures(features, zoomLevel, shouldAnimate);
}
}
/**
* Zoom to features, gets coordinates of features and creates bounding extent
* @param features - list of features to zoom in, if empty all features will be zoomed to
* @param zoomLevel - number for level to zoom in
*/
zoomToFeatures(features: Feature[], zoomLevel: number, shouldAnimate = true): void {
if (!features.length) {
features = this.features;
}
const coordinates: Coordinate[] = [];
for (const f of features) {
const coordinate = this.getFeatureCoordinate(f);
Eif (coordinate) {
coordinates.push(coordinate);
}
}
this.zoomToCoordinates(coordinates, zoomLevel, shouldAnimate, this.fitPointPadding());
}
/**
* Zoom to GeoJson using coordinates of point of click
* @param coordinate - coordinates of point of click
* @param zoomLevel - number for level to zoom in
*/
zoomToGeoJson(coordinate: Coordinate, zoomLevel: number, shouldAnimate = true): void {
this.zoomToCoordinates([coordinate], zoomLevel, shouldAnimate, this.fitGeoJsonPadding());
}
private zoomToCoordinates(
coordinates: Coordinate[],
maxZoom: number,
animate = true,
padding?: number[]
): void {
this.ngZone.runOutsideAngular(() => {
if (this.map && coordinates.length) {
const view = this.map.getView();
view.fit(olExtent.boundingExtent(coordinates), {
duration: animate && this.centerValid() ? ANIMATION_DURATION : undefined,
maxZoom,
padding
});
}
});
}
/**
* Refresh points with new set of points
* @param points - to refresh with
*/
refresh(points: MapPoint[], zoomToFeatures = true): void {
this.points.set(points);
this.ngZone.runOutsideAngular(() => this.doRefresh(points, zoomToFeatures));
}
private doRefresh(points: MapPoint[], zoomToFeatures: boolean): void {
Iif (!this.map) {
return;
}
this.mapService.resetCaches();
this.isClicked = false;
this.clear();
const layers = this.map.getLayers().getArray() as Layer[];
this.features = this.mapService.createFeatures(points, this.alwaysShowLabels());
layers.forEach((layer: Layer) => {
const source = layer.getSource();
if (source instanceof Cluster && this.cluster()) {
const vectorSource = source.getSource();
vectorSource?.addFeatures(this.features);
E} else if (source instanceof VectorSource) {
source.addFeatures(this.features);
}
});
Eif (zoomToFeatures) {
this.zoomToFeatures([], this.globalZoom());
}
// Emit change event to notify consumers that the actual features are available.
// This is necessary to call methods like `zoomToPoints()`
this.ngZone.run(() => this.pointsRefreshed.emit());
}
clear(): void {
this.ngZone.runOutsideAngular(() => this.doClear());
}
private doClear(): void {
Iif (!this.map) {
return;
}
this.mapService.resetCaches();
this.hideOverlays();
const layers = this.map.getLayers().getArray() as Layer[];
layers.forEach((layer: Layer) => {
const source = layer.getSource();
if (source instanceof Cluster && this.cluster()) {
const vectorSource = source.getSource();
vectorSource?.clear();
}
Eif (source instanceof VectorSource) {
source.clear();
}
});
}
/**
* Selecting the certain point on map with API
* @param point - which select
*/
select(point: MapPoint, maxZoomLevel?: number): void {
this.ngZone.runOutsideAngular(() => this.doSelect(point, maxZoomLevel));
}
private doSelect(point: MapPoint, maxZoomLevel?: number): void {
Iif (!this.map) {
return;
}
const featPoint: Feature[] = this.mapService.createFeatures([point], this.alwaysShowLabels());
const ext = featPoint[0]?.getGeometry()?.getExtent();
Eif (ext) {
const center = olExtent.getCenter(ext);
this.map.setView(
new View({
center: [center[0], center[1]],
zoom: maxZoomLevel ?? this.featureSelectZoom()
})
);
this.featureClick(featPoint[0]);
this.emitSelection(featPoint);
}
}
/** API for feature clicking with extra properties. */
featureClick(feature: Feature, event?: any, zoom?: boolean): void {
this.targetFeature = feature;
this.isClicked = true;
this.tooltipOverlay.setPosition(undefined);
this.setPopOver(feature, zoom, event);
}
private setPopOver(feature: Feature, zoom: boolean | undefined, event: any): void {
const extraProperties = feature.getProperties().extraProps;
const geometry = feature?.getGeometry();
/*If a Point geometry is present on top of a GeoJson layer, and when clicked on the point,
"else if" statement will prevent repetition of the popover render and zoom method.*/
if (geometry && geometry instanceof Point) {
Iif (zoom) {
this.zoomToFeatures([feature], this.featureClickZoom());
}
const coordinates = geometry.getCoordinates();
this.selected = feature;
this.popoverOverlay.setPosition(coordinates);
this.popoverOverlay.setOffset(this.mapService.getMarkerOffset(feature));
this.popover().render({
component: this.popoverComponent(),
mapPoints: feature.getProperties() as MapPointMetaData
});
Eif (extraProperties) {
feature.getProperties().click?.(extraProperties);
}
E} else if (this.geoJson()) {
const coordinates = event.coordinate;
this.popoverOverlay.setPosition(coordinates);
this.popoverOverlay.setOffset(this.mapService.getMarkerOffset(feature));
this.popover().render({
component: this.popoverComponent(),
mapPoints: [feature.getProperties() as MapPointMetaData]
});
if (zoom) {
this.zoomToGeoJson(coordinates, this.featureClickZoom());
}
}
}
/**
* Show tooltip with content on hover over feature
* @param feature - to show popup on
*/
showTooltipContent(feature: Feature): boolean {
const coordinates = this.getFeatureCoordinate(feature);
Eif (coordinates) {
this.tooltip().setTooltip(feature.getProperties().name as string);
this.tooltipOverlay.setOffset(this.mapService.getMarkerOffset(feature));
this.tooltipOverlay.setPosition(coordinates);
return true;
}
return false;
}
/** Sends data into cluster tooltip based on number of
* @param features - in it
* */
showClusterTooltipContent(features: Feature[], cluster?: Feature): boolean {
const coordinates =
this.getFeatureCoordinate(cluster) ?? this.calculateClusterCoordinates(features);
const labels = features.map(f => f.getProperties().name);
this.tooltip().setTooltip(labels);
this.tooltipOverlay.setOffset([0, 20]);
this.tooltipOverlay.setPosition(coordinates);
return true;
}
hideOverlays(): void {
this.tooltipOverlay.setPosition(undefined);
this.popoverOverlay.setPosition(undefined);
}
private getFeatureCoordinate(feature?: Feature): Coordinate | undefined {
const geometry = feature?.getGeometry();
if (geometry instanceof Point) {
return geometry.getCoordinates();
}
return undefined;
}
/**
* Calculates cluster center coordinates (lon, lat) for cluster tooltip positioning base on
* @param features - (lon, lan)
*/
private calculateClusterCoordinates(features: Feature[]): [number, number] {
let lon = 0;
let lat = 0;
for (const feat of features) {
const coordinates = this.getFeatureCoordinate(feat);
Eif (coordinates) {
lon += coordinates[0];
lat += coordinates[1];
}
}
return [lon / features.length, lat / features.length];
}
private showClusterPopover(cluster: Feature<Geometry>): void {
if (!cluster) {
this.popoverOverlay.setPosition(undefined);
return;
}
// Only show cluster popover when auto zoom on cluster click is disabled or when the zoom level is sufficient.
const zoom = this.map?.getView().getZoom();
const show = !this.autoZoomClusterClick() || (zoom && zoom >= this.clusterClickZoom());
if (!show) {
return;
}
this.popoverOverlay.setOffset([0, 20]);
this.popoverOverlay.setPosition(this.getFeatureCoordinate(cluster));
const features = cluster.getProperties().features as Feature<Geometry>[];
this.popover().render({
component: this.clusterPopoverComponent(),
mapPoints: features.map(f => f.getProperties() as MapPointMetaData),
map: this.map
});
// Remember the selected cluster to avoid showing the a tooltip for cluster which has an open popover.
this.selectedCluster = cluster;
}
}
|