Skip to content

Commit 7b803af

Browse files
committed
fix: projection control
1 parent b919a1b commit 7b803af

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

src/Map.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,9 +1736,9 @@ export class Map extends maplibregl.Map {
17361736
* Returns whether a globe projection is currently being used
17371737
*/
17381738
isGlobeProjection(): boolean {
1739-
const projection = this.getProjection();
1739+
const projection = this.style.projection?.name;
17401740

1741-
return projection.type === "globe";
1741+
return projection === "globe";
17421742
}
17431743

17441744
/**

src/MaptilerProjectionControl.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,17 @@ export class MaptilerProjectionControl implements IControl {
1616
projectionButton!: HTMLButtonElement;
1717
private externalProjection?: HTMLElement;
1818
private options: MaptilerProjectionControlOptions;
19-
private boundToggleProjection: (e: Event) => void;
2019

2120
constructor(options: MaptilerProjectionControlOptions = {}) {
2221
this.options = options;
2322
this.externalProjection = options.projectionElement;
24-
// Bind the method once in constructor to maintain reference
25-
this.boundToggleProjection = this.toggleProjection.bind(this);
26-
if (options.removeDefaultDOM) {
27-
this.setupExternalElements();
28-
}
2923
}
3024

3125
onAdd(map: SDKMap): HTMLElement {
3226
this.map = map;
27+
3328
if (this.options.removeDefaultDOM) {
29+
this.setupExternalElements();
3430
this.container = DOMcreate("div");
3531
return this.container;
3632
}
@@ -47,41 +43,39 @@ export class MaptilerProjectionControl implements IControl {
4743
this.projectionButton,
4844
).setAttribute("aria-hidden", "true");
4945
this.projectionButton.type = "button";
50-
this.projectionButton.addEventListener("click", this.boundToggleProjection);
46+
this.projectionButton.addEventListener("click", this.toggleProjection);
5147

5248
map.on("projectiontransition", this.updateProjectionIcon.bind(this));
5349

5450
this.updateProjectionIcon();
51+
5552
return this.container;
5653
}
5754

5855
onRemove(): void {
5956
if (this.externalProjection) {
6057
this.externalProjection.removeEventListener(
6158
"click",
62-
this.boundToggleProjection,
59+
this.toggleProjection,
6360
);
6461
}
6562
DOMRemove(this.container);
66-
this.map.off("projectiontransition", this.updateProjectionIcon);
63+
this.map.off("projectiontransition", this.updateProjectionIcon.bind(this));
6764
// @ts-expect-error: map will only be undefined on remove
6865
this.map = undefined;
6966
}
7067

7168
private setupExternalElements(): void {
7269
if (this.externalProjection) {
73-
this.externalProjection.addEventListener(
74-
"click",
75-
this.boundToggleProjection,
76-
);
70+
this.externalProjection.addEventListener("click", this.toggleProjection);
7771
// Set initial title
7872
this.updateExternalTitle();
7973
// Listen for projection changes
8074
this.map.on("projectiontransition", this.updateExternalTitle.bind(this));
8175
}
8276
}
8377

84-
private toggleProjection(): void {
78+
private toggleProjection = () => {
8579
if (this.map.isGlobeProjection()) {
8680
this.map.enableMercatorProjection();
8781
} else {
@@ -90,7 +84,7 @@ export class MaptilerProjectionControl implements IControl {
9084
if (!this.options.removeDefaultDOM) {
9185
this.updateProjectionIcon();
9286
}
93-
}
87+
};
9488

9589
private updateExternalTitle(): void {
9690
if (this.externalProjection) {

0 commit comments

Comments
 (0)