Skip to content

Commit

Permalink
Merge pull request #145 from OutSystems/ROU-4591
Browse files Browse the repository at this point in the history
ROU-4591: change how addresses are transformed into coordenates
  • Loading branch information
rugoncalves authored Nov 6, 2023
2 parents 777890a + 4f68e5b commit 311a9a8
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 51 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@
},
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"sonarlint.connectedMode.project": {
"connectionId": "outsystems",
"projectKey": "OutSystems_outsystems-maps"
}
}
}
5 changes: 1 addition & 4 deletions src/Providers/Maps/Google/Features/Center.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ namespace Provider.Maps.Google.Feature {
}

public updateCenter(location: string): void {
Helper.Conversions.ConvertToCoordinates(
location,
this._map.config.apiKey
)
Helper.Conversions.ConvertToCoordinates(location)
.then((response) => {
this._map.config.center = response;
this._initialCenter = response;
Expand Down
67 changes: 38 additions & 29 deletions src/Providers/Maps/Google/Helper/Conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,43 @@ namespace Provider.Maps.Google.Helper.Conversions {
* @returns Promise that will retrieve the coordinates
*/
const googleMapsApiGeocode = function (
location: string,
apiKey: string
location: string
): Promise<OSFramework.Maps.OSStructures.OSMap.Coordinates> {
// Encodes a location string into a valid format
const encoded_location = encodeURIComponent(location);
return fetch(
`${OSFramework.Maps.Helper.Constants.googleMapsApiGeocode}?address=${encoded_location}&key=${apiKey}`
)
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error("Server response wasn't OK");
}
})
.then((json) => {
if (json.results.length === 0) {
console.warn(
`No results have been found for address "${location}".`
);
throw new Error(
json.error_message ?? 'No results have been found.'
);
}
const loc = json.results[0].geometry.location;
return { lat: loc.lat, lng: loc.lng };
});
const geo = new google.maps.Geocoder();

return new Promise<OSFramework.Maps.OSStructures.OSMap.Coordinates>(
(resolve, reject) => {
geo.geocode(
{ address: location },
(
results: google.maps.GeocoderResult[],
status: google.maps.GeocoderStatus
) => {
if (status === google.maps.GeocoderStatus.OK) {
const loc = results[0].geometry.location;
resolve({ lat: loc.lat(), lng: loc.lng() });
} else if (
status === google.maps.GeocoderStatus.ZERO_RESULTS
) {
console.warn(
`No results have been found for address "${location}".`
);
reject('No results have been found.');
} else if (
status ===
google.maps.GeocoderStatus.OVER_QUERY_LIMIT
) {
reject(
'Google Maps API call limit exceeded. Please wait a few moments and try again'
);
} else {
reject(status);
}
}
);
}
);
};
/**
* Promise that will retrive the coordinates for a specific location via Google Maps API
Expand All @@ -44,8 +54,7 @@ namespace Provider.Maps.Google.Helper.Conversions {
* @returns Promise that will retrieve the coordinates
*/
export function ConvertToCoordinates(
location: string,
apiKey: string
location: string
): Promise<OSFramework.Maps.OSStructures.OSMap.Coordinates> {
if (location === undefined || location.trim().length === 0) {
console.warn(
Expand Down Expand Up @@ -73,12 +82,12 @@ namespace Provider.Maps.Google.Helper.Conversions {
});
} else {
// Try to get the address via the googleMapsAPIGeocode
return googleMapsApiGeocode(location, apiKey);
return googleMapsApiGeocode(location);
}
}
// If the location is an address try to get the address via the googleMapsAPIGeocode
else {
return googleMapsApiGeocode(location, apiKey);
return googleMapsApiGeocode(location);
}
}
}
6 changes: 2 additions & 4 deletions src/Providers/Maps/Google/Marker/Marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ namespace Provider.Maps.Google.Marker {
// Let's return a promise that will be resolved or rejected according to the result
return new Promise((resolve, reject) => {
Helper.Conversions.ConvertToCoordinates(
this.config.location,
this.map.config.apiKey
this.config.location
)
.then((response) => {
markerOptions.position = {
Expand Down Expand Up @@ -234,8 +233,7 @@ namespace Provider.Maps.Google.Marker {
switch (propValue) {
case OSFramework.Maps.Enum.OS_Config_Marker.location:
Helper.Conversions.ConvertToCoordinates(
propertyValue as string,
this.map.config.apiKey
propertyValue as string
)
.then((response) => {
this._provider.setPosition({
Expand Down
5 changes: 1 addition & 4 deletions src/Providers/Maps/Google/Shape/AbstractPolyshape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ namespace Provider.Maps.Google.Shape {
return false;
}

Helper.Conversions.ConvertToCoordinates(
location,
this.map.config.apiKey
)
Helper.Conversions.ConvertToCoordinates(location)
.then((response) => {
shapePath.set(index, {
lat: response.lat,
Expand Down
5 changes: 1 addition & 4 deletions src/Providers/Maps/Google/Shape/Circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ namespace Provider.Maps.Google.Shape {
}

return new Promise((resolve, reject) => {
Helper.Conversions.ConvertToCoordinates(
location,
this.map.config.apiKey
)
Helper.Conversions.ConvertToCoordinates(location)
.then((response) => {
const coordinates = {
lat: response.lat,
Expand Down
5 changes: 1 addition & 4 deletions src/Providers/Maps/Google/Shape/Rectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ namespace Provider.Maps.Google.Shape {
resolve(newBounds);
}
} else {
Helper.Conversions.ConvertToCoordinates(
bounds[cd],
this.map.config.apiKey
)
Helper.Conversions.ConvertToCoordinates(bounds[cd])
.then((response) => {
boundsLength++;
switch (cd) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace Provider.Maps.Google.SharedComponents {
resolve(finalBounds);
}
} else {
Helper.Conversions.ConvertToCoordinates(bounds[cd], apiKey)
Helper.Conversions.ConvertToCoordinates(bounds[cd])
.then((response) => {
// Make sure to increment the boundsLength to validate whether the bounds structure has all the cardinal directions or not.
boundsLength++;
Expand Down

0 comments on commit 311a9a8

Please sign in to comment.