Skip to content

Commit 5e5bd23

Browse files
- add flag to refresh the center position if the center was changed by the user; (#188)
- clean and normalise refresh function on both providers;
1 parent 86d4f42 commit 5e5bd23

5 files changed

Lines changed: 21 additions & 16 deletions

File tree

src/OSFramework/Maps/OSMap/IMap.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ namespace OSFramework.Maps.OSMap {
157157
/**
158158
* Refreshes the Map after changing zoom or center.
159159
* Can be used to reset to the defined zoom, center and offset configurations.
160+
* @param {boolean} [centerchanged]
160161
*/
161-
refresh(): void;
162+
refresh(centerchanged?: boolean): void;
162163
/**
163164
* Refreshes the Events of the Map Provider after Subscribing/Unsubscribing events
164165
*/

src/Providers/Maps/Google/Features/Center.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace Provider.Maps.Google.Feature {
6767
.then((response) => {
6868
this._map.config.center = response;
6969
this._initialCenter = response;
70-
this._map.refresh();
70+
this._map.refresh(true);
7171
})
7272
.catch((error) => {
7373
this._map.mapEvents.trigger(

src/Providers/Maps/Google/OSMap/OSMap.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ namespace Provider.Maps.Google.OSMap {
426426
this._provider = undefined;
427427
}
428428

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

@@ -439,35 +439,35 @@ namespace Provider.Maps.Google.OSMap {
439439
//If there are markers, let's choose the map center accordingly.
440440
//Otherwise, the map center will be the one defined in the configs.
441441
if (this.markers.length > 0) {
442+
// The TS definitions appear to be outdated.
443+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
444+
const markerProvider: any = this.markers[0].provider;
442445
if (this.markers.length > 1) {
443446
//As the map has more than one marker, let's see if the map
444447
//center should be changed.
445448
//If the user hasn't change zoom, or the developer is ignoring it (current behavior).
446449
if (this.allowRefreshZoom) {
447-
const markerProvider = this.markers[0].provider;
448450
//Let's check if the marker provider is ready to be used.
449451
if (markerProvider !== undefined) {
450452
//If the map center, is the same as the default, then the map will ignore it.
451453
//Otherwise, the isAutofit config will be checked, and if false, then the current
452454
//center will not be changed.
453455
if (isDefault || this.features.zoom.isAutofit) {
454456
//Let's use the first marker as the center of the map.
455-
// The TS definitions appear to be outdated.
456-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
457-
position = (markerProvider as any).position.toJSON();
457+
position = markerProvider.position.toJSON();
458458
}
459459
}
460460
} else {
461461
//If the user has zoomed and the developer intends to respect user zoom
462462
//then the current map center will be used.
463-
position = this.provider.getCenter().toJSON();
463+
position = centerChanged
464+
? (this.config.center as OSFramework.Maps.OSStructures.OSMap.Coordinates)
465+
: this.provider.getCenter().toJSON();
464466
}
465-
} else if (this.markers[0].provider !== undefined) {
467+
} else if (markerProvider !== undefined) {
466468
//If there's only one marker, and is already created, its location will be
467469
//used as the map center.
468-
// The TS definitions appear to be outdated.
469-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
470-
position = (this.markers[0].provider as any).position.toJSON();
470+
position = markerProvider.position.toJSON();
471471
}
472472
}
473473

src/Providers/Maps/Leaflet/Features/Center.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace Provider.Maps.Leaflet.Feature {
8787
.then((response) => {
8888
this._map.config.center = response;
8989
this._initialCenter = response;
90-
this._map.refresh();
90+
this._map.refresh(true);
9191
})
9292
.catch(() => {
9393
this._map.mapEvents.trigger(

src/Providers/Maps/Leaflet/OSMap/OSMap.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ namespace Provider.Maps.Leaflet.OSMap {
260260
this._provider = undefined;
261261
}
262262

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

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

0 commit comments

Comments
 (0)