Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROU-4665: allow preserving the zoom level when user changes it #152

Merged
merged 8 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/OSFramework/Maps/Configuration/IConfigurationMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace OSFramework.Maps.Configuration {
apiKey?: string;
center: string | OSStructures.OSMap.Coordinates;
height: string;
respectUserZoom?: boolean;
type?: Enum.OSMap.Type;
uniqueId: string;
zoom: Enum.OSMap.Zoom;
Expand Down
1 change: 1 addition & 0 deletions src/OSFramework/Maps/Enum/OS_Config_Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace OSFramework.Maps.Enum {
markerClustererMinClusterSize,
markerClustererZoomOnClick,
offset,
respectUserZoom,
showTraffic,
staticMap,
style,
Expand Down
21 changes: 21 additions & 0 deletions src/OSFramework/Maps/OSMap/AbstractMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ namespace OSFramework.Maps.OSMap {
private _shapesSet: Set<Shape.IShape>;
private _uniqueId: string;
private _widgetId: string;
private _zoomChanged: boolean;

protected _features: Feature.ExposedFeatures;
protected _mapZoomChangeCallback: Maps.Callbacks.Generic;
protected _provider: W;

constructor(
Expand All @@ -46,9 +48,15 @@ namespace OSFramework.Maps.OSMap {
this._mapEvents = new Event.OSMap.MapEventsManager(this);
this._mapType = mapType;
this._providerType = providerType;
this._zoomChanged = false;
this._mapZoomChangeCallback = this._mapZoomChangeHandler.bind(this);
}
public abstract get mapTag(): string;

protected get allowRefreshZoom(): boolean {
return !(this.config.respectUserZoom && this._zoomChanged);
}

protected get shapes(): Shape.IShape[] {
return Array.from(this._shapesSet);
}
Expand Down Expand Up @@ -109,6 +117,12 @@ namespace OSFramework.Maps.OSMap {
return this._widgetId;
}

private _mapZoomChangeHandler(): void {
if (this.config.respectUserZoom) {
this._zoomChanged = true;
}
}

protected finishBuild(): void {
this._isReady = true;

Expand Down Expand Up @@ -168,6 +182,13 @@ namespace OSFramework.Maps.OSMap {
//Update Map's config when the property is available
if (this.config.hasOwnProperty(propertyName)) {
this.config[propertyName] = propertyValue;

if (
Maps.Enum.OS_Config_Map.respectUserZoom ===
Maps.Enum.OS_Config_Map[propertyName]
) {
this._zoomChanged = false;
}
} else {
this.mapEvents.trigger(
Event.OSMap.MapEventType.OnError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Provider.Maps.Google.Configuration.OSMap {
public height: string;
public markerClusterer: OSFramework.Maps.Configuration.IConfigurationMarkerClusterer;
public offset: OSFramework.Maps.OSStructures.OSMap.Offset;
public respectUserZoom: boolean;
public showTraffic: boolean;
public style: OSFramework.Maps.Enum.OSMap.Style;
public type: OSFramework.Maps.Enum.OSMap.Type;
Expand Down
40 changes: 38 additions & 2 deletions src/Providers/Maps/Google/OSMap/OSMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Provider.Maps.Google.OSMap {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _advancedFormatObj: any;
private _fBuilder: Feature.FeatureBuilder;
private _gZoomChangeListener: google.maps.MapsEventListener;
private _scriptCallback: OSFramework.Maps.Callbacks.Generic;

constructor(mapId: string, configs: JSON) {
Expand All @@ -26,6 +27,26 @@ namespace Provider.Maps.Google.OSMap {
this._scriptCallback = this._createGoogleMap.bind(this);
}

private _addMapZoomHandler(): void {
/*
This timeout is here due to an anomaly in the behaviour of google maps
when more than one maker is added to the map, the zoom event is fired twice:
https://stackoverflow.com/questions/20436185/google-maps-api-v3-zoom-changed-event-fired-twice-after-fitbounds-called

However since the event is triggered asynchronously and there's no way
to wait for it, or to know it will occur, this hack was added. I'm sorry, I'm ashamed of myself.
*/
setTimeout(() => {
if (this && this._provider) {
this._gZoomChangeListener = google.maps.event.addListener(
this._provider,
Constants.OSMap.ProviderEventNames.zoom_changed,
this._mapZoomChangeCallback
);
}
}, 100);
}

private _buildDrawingTools(): void {
// There is only one drawingTools per map
this.drawingTools && this.drawingTools.build();
Expand Down Expand Up @@ -113,6 +134,7 @@ namespace Provider.Maps.Google.OSMap {

// We can only set the events on the provider after its creation
this._setMapEvents(this._advancedFormatObj.mapEvents);
this._addMapZoomHandler();
} else {
throw Error(`The google.maps lib has not been loaded.`);
}
Expand All @@ -130,6 +152,12 @@ namespace Provider.Maps.Google.OSMap {
return this.config.getProviderConfig();
}

private _removeMapZoomHandler(): void {
if (this._gZoomChangeListener) {
google.maps.event.removeListener(this._gZoomChangeListener);
}
}

private _setMapEvents(events?: Array<string>): void {
SharedComponents.RemoveEventsFromProvider(this);

Expand Down Expand Up @@ -450,6 +478,9 @@ namespace Provider.Maps.Google.OSMap {
}

public refresh(): void {
//Let's stop listening to the zoom event be caused by the refreshZoom
rugoncalves marked this conversation as resolved.
Show resolved Hide resolved
this._removeMapZoomHandler();

let position = this.features.center.getCenter();
// When the position is empty, we use the default position
// If the configured center position of the map is equal to the default
Expand Down Expand Up @@ -488,15 +519,20 @@ namespace Provider.Maps.Google.OSMap {
// Refresh the center position
this.features.center.refreshCenter(position);

// Refresh the zoom
this.features.zoom.refreshZoom();
if (this.allowRefreshZoom) {
// Refresh the zoom
this.features.zoom.refreshZoom();
}

// Refresh the offset
this.features.offset.setOffset(this.features.offset.getOffset);

// Repaint the marker Clusterers
this.hasMarkerClusterer() &&
this.features.markerClusterer.repaint();

//Re-adding the map zoom handler
rugoncalves marked this conversation as resolved.
Show resolved Hide resolved
this._addMapZoomHandler();
}

public refreshProviderEvents(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Provider.Maps.Leaflet.Configuration.OSMap {
public center: string | OSFramework.Maps.OSStructures.OSMap.Coordinates;
public height: string;
public offset: OSFramework.Maps.OSStructures.OSMap.Offset;
public respectUserZoom: boolean;
rugoncalves marked this conversation as resolved.
Show resolved Hide resolved
public uniqueId: string;
public zoom: OSFramework.Maps.Enum.OSMap.Zoom;

Expand Down
1 change: 1 addition & 0 deletions src/Providers/Maps/Leaflet/Constants/OSMap/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Provider.Maps.Leaflet.Constants.OSMap {
tilesloaded = 'load',
load = 'load',
zoom_changed = 'zoom',
zoom_end = 'zoomend',
zoom = 'zoom'
}
}
26 changes: 24 additions & 2 deletions src/Providers/Maps/Leaflet/OSMap/OSMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ namespace Provider.Maps.Leaflet.OSMap {
);
}

private _addMapZoomHandler(): void {
this._provider.on(
Constants.OSMap.ProviderEventNames.zoom_end,
this._mapZoomChangeCallback
);
}

private _buildDrawingTools(): void {
// Here we aren't using a forEach because there is only one drawingTools per map
this.drawingTools && this.drawingTools.build();
Expand All @@ -53,6 +60,13 @@ namespace Provider.Maps.Leaflet.OSMap {
return this.config.getProviderConfig();
}

private _removeMapZoomHandler(): void {
this._provider.off(
Constants.OSMap.ProviderEventNames.zoom_end,
this._mapZoomChangeCallback
);
}

private _setMapEvents(): void {
// Any events that got added to the mapEvents via the API Subscribe method will have to be taken care here
// If the Event type of each handler is MapProviderEvent, we want to make sure to add that event to the listeners of the google maps provider (e.g. click, dblclick, contextmenu, etc)
Expand Down Expand Up @@ -189,6 +203,8 @@ namespace Provider.Maps.Leaflet.OSMap {
// Make sure to change the center after the conversion of the location to coordinates
this.features.center.updateCenter(currentCenter as string);
this._setMapEvents();

this._addMapZoomHandler();
}

public buildFeatures(): void {
Expand Down Expand Up @@ -317,6 +333,8 @@ namespace Provider.Maps.Leaflet.OSMap {
}

public refresh(): void {
this._removeMapZoomHandler();

let position = this.features.center.getCenter();
// When the position is empty, we use the default position
// If the configured center position of the map is equal to the default
Expand Down Expand Up @@ -355,15 +373,19 @@ namespace Provider.Maps.Leaflet.OSMap {
// Refresh the center position
this.features.center.refreshCenter(position);

// Refresh the zoom
this.features.zoom.refreshZoom();
if (this.allowRefreshZoom) {
// Refresh the zoom
this.features.zoom.refreshZoom();
}

// Refresh the offset
this.features.offset.setOffset(this.features.offset.getOffset);

// Repaint the marker Clusterers
this.hasMarkerClusterer() &&
this.features.markerClusterer.repaint();

this._addMapZoomHandler();
}

public refreshProviderEvents(): void {
Expand Down
Loading