Skip to content

Commit

Permalink
Merge pull request #33 from starschema/persist-zoom-info
Browse files Browse the repository at this point in the history
Persist current coordinates and zoom when auto zoom is turned off
  • Loading branch information
szilardhuber authored Aug 26, 2022
2 parents b0b072e + dc8867f commit b96cae9
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/autoZoomControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,7 @@ export class AutoZoomControl implements mapboxgl.IControl {
this.toggled = !this.toggled;
this.zoomPinButton.className = this.getButtonClass();
this.zoomPinButton.title = this.getButtonTitle();
// Persist autozoom state into 'api' settings
this.host.persistProperties(<VisualObjectInstancesToPersist>{
merge: [{
objectName: "api",
selector: null,
properties: {
autozoom: !this.toggled,
}
}]
})
this.persistCurrentZoomInfo()
});
return this.container;
}
Expand All @@ -55,10 +46,28 @@ export class AutoZoomControl implements mapboxgl.IControl {

public setPin(state) {
this.toggled = state;
this.persistCurrentZoomInfo()
this.zoomPinButton.className = this.getButtonClass();
this.zoomPinButton.title = this.getButtonTitle();
}

private persistCurrentZoomInfo() {
// Persist autozoom state into 'api' settings
const coordinates = this.map.getCenter()
this.host.persistProperties(<VisualObjectInstancesToPersist>{
merge: [{
objectName: "api",
selector: null,
properties: {
autozoom: !this.toggled,
zoom: this.toggled ? Math.floor(this.map.getZoom()) : 0,
startLong: this.toggled ? coordinates.lng : 0,
startLat: this.toggled ? coordinates.lat : 0,
}
}]
})
}

private createButton(className: string, ariaLabel: string, fn: () => any) {
const button = this.createElement('button', className, this.container);
button.type = 'button';
Expand Down

0 comments on commit b96cae9

Please sign in to comment.