Skip to content

Commit

Permalink
- add flag to refresh the center position if the center was changed b…
Browse files Browse the repository at this point in the history
…y the user;

- clean and normalise refresh function on both providers;
  • Loading branch information
JoaoFerreira-FrontEnd committed Nov 26, 2024
1 parent 86d4f42 commit cd7b544
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/OSFramework/Maps/OSMap/IMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ namespace OSFramework.Maps.OSMap {
/**
* Refreshes the Map after changing zoom or center.
* Can be used to reset to the defined zoom, center and offset configurations.
* @param {boolean} [centerchanged]
*/
refresh(): void;
refresh(centerchanged?: boolean): void;
/**
* Refreshes the Events of the Map Provider after Subscribing/Unsubscribing events
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Maps/Google/Features/Center.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace Provider.Maps.Google.Feature {
.then((response) => {
this._map.config.center = response;
this._initialCenter = response;
this._map.refresh();
this._map.refresh(true);
})
.catch((error) => {
this._map.mapEvents.trigger(
Expand Down
20 changes: 10 additions & 10 deletions src/Providers/Maps/Google/OSMap/OSMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ namespace Provider.Maps.Google.OSMap {
this._provider = undefined;
}

public refresh(): void {
public refresh(centerChanged?: boolean): void {
//Let's stop listening to the zoom event be caused by the refreshZoom
this._removeMapZoomHandler();

Expand All @@ -439,35 +439,35 @@ namespace Provider.Maps.Google.OSMap {
//If there are markers, let's choose the map center accordingly.
//Otherwise, the map center will be the one defined in the configs.
if (this.markers.length > 0) {
// The TS definitions appear to be outdated.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const markerProvider: any = this.markers[0].provider;
if (this.markers.length > 1) {
//As the map has more than one marker, let's see if the map
//center should be changed.
//If the user hasn't change zoom, or the developer is ignoring it (current behavior).
if (this.allowRefreshZoom) {
const markerProvider = this.markers[0].provider;
//Let's check if the marker provider is ready to be used.
if (markerProvider !== undefined) {
//If the map center, is the same as the default, then the map will ignore it.
//Otherwise, the isAutofit config will be checked, and if false, then the current
//center will not be changed.
if (isDefault || this.features.zoom.isAutofit) {
//Let's use the first marker as the center of the map.
// The TS definitions appear to be outdated.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
position = (markerProvider as any).position.toJSON();
position = markerProvider.position.toJSON();
}
}
} else {
//If the user has zoomed and the developer intends to respect user zoom
//then the current map center will be used.
position = this.provider.getCenter().toJSON();
position = centerChanged
? (this.config.center as OSFramework.Maps.OSStructures.OSMap.Coordinates)
: this.provider.getCenter().toJSON();
}
} else if (this.markers[0].provider !== undefined) {
} else if (markerProvider !== undefined) {
//If there's only one marker, and is already created, its location will be
//used as the map center.
// The TS definitions appear to be outdated.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
position = (this.markers[0].provider as any).position.toJSON();
position = markerProvider.position.toJSON();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Maps/Leaflet/Features/Center.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace Provider.Maps.Leaflet.Feature {
.then((response) => {
this._map.config.center = response;
this._initialCenter = response;
this._map.refresh();
this._map.refresh(true);
})
.catch(() => {
this._map.mapEvents.trigger(
Expand Down
10 changes: 7 additions & 3 deletions src/Providers/Maps/Leaflet/OSMap/OSMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ namespace Provider.Maps.Leaflet.OSMap {
this._provider = undefined;
}

public refresh(): void {
public refresh(centerChanged?: boolean): void {
//Let's stop listening to the zoom event be caused by the refreshZoom
this._removeMapZoomHandler();

Expand All @@ -277,11 +277,15 @@ namespace Provider.Maps.Leaflet.OSMap {
//If the user hasn't change zoom, or the developer is ignoring
//it (current behavior), then the map will be centered tentatively
//in the first marker.
position = markerProvider.getLatLng();
if (this.features.zoom.isAutofit) {
position = markerProvider.getLatLng();
}
} else {
//If the user has zoomed and the developer intends to respect user zoom
//then the current map center will be used.
position = this.provider.getCenter();
position = centerChanged
? (this.config.center as OSFramework.Maps.OSStructures.OSMap.Coordinates)
: this.provider.getCenter();
}
} else if (markerProvider !== undefined) {
//If there's only one marker, and is already created, its location will be
Expand Down

0 comments on commit cd7b544

Please sign in to comment.