Skip to content

Commit

Permalink
Merge pull request #172 from OutSystems/ROU-4763
Browse files Browse the repository at this point in the history
ROU-4763: Add ability to to GoogleMaps to add localization by setting the language and region
  • Loading branch information
bmarcelino-fe authored Apr 3, 2024
2 parents 327f50a + e2835cc commit 1d4f0eb
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace OSFramework.Maps.Configuration {
export interface IConfigurationSearchPlaces extends IConfiguration {
apiKey: string;
countries: Array<string>;
localization: OSFramework.Maps.OSStructures.OSMap.Localization;
searchArea: OSStructures.OSMap.BoundsString;
searchType: Enum.SearchTypes;
}
Expand Down
2 changes: 2 additions & 0 deletions src/OSFramework/Maps/Enum/ErrorCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ namespace OSFramework.Maps.Enum {
export enum ErrorCodes {
// Error Codes - CONFiguration errors - Any error related with missing or wrong configuration of the application.
CFG_APIKeyAlreadySetMap = 'MAPS-CFG-01001',
CFG_LocalizationAlreadySetMap = 'MAPS-CFG-01003',
CFG_APIKeyAlreadySetStaticMap = 'MAPS-CFG-02001',
CFG_CantChangeParamsStaticMap = 'MAPS-CFG-02002',
CFG_LocalizationAlreadySetStaticMap = 'MAPS-CFG-02003',
CFG_InvalidPolylineShapeLocations = 'MAPS-CFG-05001',
CFG_InvalidPolygonShapeLocations = 'MAPS-CFG-05002',
CFG_InvalidCircleShapeCenter = 'MAPS-CFG-05003',
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 @@ -8,6 +8,7 @@ namespace OSFramework.Maps.Enum {
apiKey,
height,
center,
localization,
markerClustererActive,
markerClustererMaxZoom,
markerClustererMinClusterSize,
Expand Down
1 change: 1 addition & 0 deletions src/OSFramework/Maps/Enum/OS_Config_SearchPlaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace OSFramework.Maps.Enum {
*/
export enum OS_Config_SearchPlaces {
apiKey,
localization,
countries,
searchArea,
searchType,
Expand Down
1 change: 1 addition & 0 deletions src/OSFramework/Maps/Enum/OS_Config_StaticMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace OSFramework.Maps.Enum {
*/
export enum OS_Config_StaticMap {
apiKey,
localization,
height,
center,
type,
Expand Down
4 changes: 4 additions & 0 deletions src/OSFramework/Maps/OSStructures/OSMap.Offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ namespace OSFramework.Maps.OSStructures.OSMap {
public offsetX: number;
public offsetY: number;
}
export class Localization {
public language: string;
public region: string;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Provider.Maps.Google.Configuration.OSMap {
public autoZoomOnShapes: boolean;
public center: string | OSFramework.Maps.OSStructures.OSMap.Coordinates;
public height: string;
public localization: OSFramework.Maps.OSStructures.OSMap.Localization;
public markerClusterer: OSFramework.Maps.Configuration.IConfigurationMarkerClusterer;
public offset: OSFramework.Maps.OSStructures.OSMap.Offset;
public respectUserZoom: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Provider.Maps.Google.Configuration.OSMap {
public apiKey: string;
public center: string | OSFramework.Maps.OSStructures.OSMap.Coordinates;
public height: string;
public localization: OSFramework.Maps.OSStructures.OSMap.Localization;
public type: OSFramework.Maps.Enum.OSMap.Type;
public uniqueId: string;
public zoom: OSFramework.Maps.Enum.OSMap.Zoom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Provider.Maps.Google.Configuration.SearchPlaces {
{
public apiKey: string;
public countries: Array<string>;
public localization: OSFramework.Maps.OSStructures.OSMap.Localization;
public searchArea: OSFramework.Maps.OSStructures.OSMap.BoundsString;
public searchType: OSFramework.Maps.Enum.SearchTypes;

Expand Down
12 changes: 11 additions & 1 deletion src/Providers/Maps/Google/OSMap/OSMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ namespace Provider.Maps.Google.OSMap {
* 1) Add the script from GoogleAPIS to the header of the page
* 2) Creates the Map via GoogleMap API
*/
SharedComponents.InitializeScripts(this.config.apiKey, this._scriptCallback);
SharedComponents.InitializeScripts(this.config.apiKey, this.config.localization, this._scriptCallback);
}

public buildFeatures(): void {
Expand Down Expand Up @@ -326,7 +326,9 @@ namespace Provider.Maps.Google.OSMap {

public changeProperty(propertyName: string, propertyValue: unknown): void {
const propValue = OSFramework.Maps.Enum.OS_Config_Map[propertyName];

super.changeProperty(propertyName, propertyValue);

if (this.isReady) {
switch (propValue) {
case OSFramework.Maps.Enum.OS_Config_Map.apiKey:
Expand All @@ -342,6 +344,14 @@ namespace Provider.Maps.Google.OSMap {
return this.features.center.updateCenter(propertyValue as string);
case OSFramework.Maps.Enum.OS_Config_Map.offset:
return this.features.offset.setOffset(JSON.parse(propertyValue as string));
case OSFramework.Maps.Enum.OS_Config_Map.localization:
// Trigger an error to alert the user that the localization can only be set one time
this.mapEvents.trigger(
OSFramework.Maps.Event.OSMap.MapEventType.OnError,
this,
OSFramework.Maps.Enum.ErrorCodes.CFG_LocalizationAlreadySetMap
);
return;
case OSFramework.Maps.Enum.OS_Config_Map.zoom:
return this.features.zoom.setLevel(propertyValue as OSFramework.Maps.Enum.OSMap.Zoom);
case OSFramework.Maps.Enum.OS_Config_Map.type:
Expand Down
4 changes: 3 additions & 1 deletion src/Providers/Maps/Google/OSMap/StaticMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ namespace Provider.Maps.Google.OSMap {
'&size=' +
this._size.width +
'x' +
this._size.height;
this._size.height +
(this.config.localization.language !== '' ? `&language=${this.config.localization.language}` : '') +
(this.config.localization.region !== '' ? `&region=${this.config.localization.region}` : '');
image.onerror = () => {
// Check if needed
this.mapEvents.trigger(
Expand Down
10 changes: 9 additions & 1 deletion src/Providers/Maps/Google/SearchPlaces/SearchPlaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ namespace Provider.Maps.Google.SearchPlaces {
* 1) Add the script from GoogleAPIS to the header of the page
* 2) Creates the SearchPlaces via GoogleMap API
*/
SharedComponents.InitializeScripts(this.config.apiKey, this._scriptCallback);
SharedComponents.InitializeScripts(this.config.apiKey, this.config.localization, this._scriptCallback);
}

public changeProperty(propertyName: string, propertyValue: unknown): void {
Expand All @@ -212,6 +212,14 @@ namespace Provider.Maps.Google.SearchPlaces {
);
}
return;
case OSFramework.Maps.Enum.OS_Config_SearchPlaces.localization:
// Trigger an error to alert the user that the localization can only be set one time
this.searchPlacesEvents.trigger(
OSFramework.Maps.Event.SearchPlaces.SearchPlacesEventType.OnError,
this,
OSFramework.Maps.Enum.ErrorCodes.CFG_LocalizationAlreadySetMap
);
return;
case OSFramework.Maps.Enum.OS_Config_SearchPlaces.searchArea:
// eslint-disable-next-line no-case-declarations
const searchArea = this._buildSearchArea(propertyValue as string);
Expand Down
12 changes: 10 additions & 2 deletions src/Providers/Maps/Google/SharedComponents/MapsAndSearchPlaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
namespace Provider.Maps.Google.SharedComponents {
let googleMapsLoadPromise = undefined;
export function InitializeScripts(apiKey: string, cb: () => void): void {
export function InitializeScripts(
apiKey: string,
localization: OSFramework.Maps.OSStructures.OSMap.Localization,
cb: () => void
): void {
if (typeof google === 'object' && typeof google.maps === 'object') {
cb();
} else {
Expand All @@ -23,14 +27,18 @@ namespace Provider.Maps.Google.SharedComponents {
resolve(0);
googleMapsLoadPromise = undefined;
};

const script = document.createElement('script');

script.src =
`${OSFramework.Maps.Helper.Constants.googleMapsApiMap}?` +
`key=${apiKey}` +
`&libraries=${OSFramework.Maps.Helper.Constants.gmlibraries}` +
`&v=${OSFramework.Maps.Helper.Constants.gmversion}` +
`&loading=async` +
`&callback=GMCB`;
`&callback=GMCB` +
(localization.language !== '' ? `&language=${localization.language}` : '') +
(localization.region !== '' ? `&region=${localization.region}` : '');
script.async = true;
script.defer = true;
script.id = OSFramework.Maps.Helper.Constants.googleMapsScript;
Expand Down

0 comments on commit 1d4f0eb

Please sign in to comment.