Skip to content

Commit 311a9a8

Browse files
authored
Merge pull request #145 from OutSystems/ROU-4591
ROU-4591: change how addresses are transformed into coordenates
2 parents 777890a + 4f68e5b commit 311a9a8

8 files changed

Lines changed: 50 additions & 51 deletions

File tree

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@
2727
},
2828
"[jsonc]": {
2929
"editor.defaultFormatter": "vscode.json-language-features"
30+
},
31+
"sonarlint.connectedMode.project": {
32+
"connectionId": "outsystems",
33+
"projectKey": "OutSystems_outsystems-maps"
3034
}
31-
}
35+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ namespace Provider.Maps.Google.Feature {
7575
}
7676

7777
public updateCenter(location: string): void {
78-
Helper.Conversions.ConvertToCoordinates(
79-
location,
80-
this._map.config.apiKey
81-
)
78+
Helper.Conversions.ConvertToCoordinates(location)
8279
.then((response) => {
8380
this._map.config.center = response;
8481
this._initialCenter = response;

src/Providers/Maps/Google/Helper/Conversions.ts

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,43 @@ namespace Provider.Maps.Google.Helper.Conversions {
88
* @returns Promise that will retrieve the coordinates
99
*/
1010
const googleMapsApiGeocode = function (
11-
location: string,
12-
apiKey: string
11+
location: string
1312
): Promise<OSFramework.Maps.OSStructures.OSMap.Coordinates> {
1413
// Encodes a location string into a valid format
15-
const encoded_location = encodeURIComponent(location);
16-
return fetch(
17-
`${OSFramework.Maps.Helper.Constants.googleMapsApiGeocode}?address=${encoded_location}&key=${apiKey}`
18-
)
19-
.then((response) => {
20-
if (response.ok) {
21-
return response.json();
22-
} else {
23-
throw new Error("Server response wasn't OK");
24-
}
25-
})
26-
.then((json) => {
27-
if (json.results.length === 0) {
28-
console.warn(
29-
`No results have been found for address "${location}".`
30-
);
31-
throw new Error(
32-
json.error_message ?? 'No results have been found.'
33-
);
34-
}
35-
const loc = json.results[0].geometry.location;
36-
return { lat: loc.lat, lng: loc.lng };
37-
});
14+
const geo = new google.maps.Geocoder();
15+
16+
return new Promise<OSFramework.Maps.OSStructures.OSMap.Coordinates>(
17+
(resolve, reject) => {
18+
geo.geocode(
19+
{ address: location },
20+
(
21+
results: google.maps.GeocoderResult[],
22+
status: google.maps.GeocoderStatus
23+
) => {
24+
if (status === google.maps.GeocoderStatus.OK) {
25+
const loc = results[0].geometry.location;
26+
resolve({ lat: loc.lat(), lng: loc.lng() });
27+
} else if (
28+
status === google.maps.GeocoderStatus.ZERO_RESULTS
29+
) {
30+
console.warn(
31+
`No results have been found for address "${location}".`
32+
);
33+
reject('No results have been found.');
34+
} else if (
35+
status ===
36+
google.maps.GeocoderStatus.OVER_QUERY_LIMIT
37+
) {
38+
reject(
39+
'Google Maps API call limit exceeded. Please wait a few moments and try again'
40+
);
41+
} else {
42+
reject(status);
43+
}
44+
}
45+
);
46+
}
47+
);
3848
};
3949
/**
4050
* Promise that will retrive the coordinates for a specific location via Google Maps API
@@ -44,8 +54,7 @@ namespace Provider.Maps.Google.Helper.Conversions {
4454
* @returns Promise that will retrieve the coordinates
4555
*/
4656
export function ConvertToCoordinates(
47-
location: string,
48-
apiKey: string
57+
location: string
4958
): Promise<OSFramework.Maps.OSStructures.OSMap.Coordinates> {
5059
if (location === undefined || location.trim().length === 0) {
5160
console.warn(
@@ -73,12 +82,12 @@ namespace Provider.Maps.Google.Helper.Conversions {
7382
});
7483
} else {
7584
// Try to get the address via the googleMapsAPIGeocode
76-
return googleMapsApiGeocode(location, apiKey);
85+
return googleMapsApiGeocode(location);
7786
}
7887
}
7988
// If the location is an address try to get the address via the googleMapsAPIGeocode
8089
else {
81-
return googleMapsApiGeocode(location, apiKey);
90+
return googleMapsApiGeocode(location);
8291
}
8392
}
8493
}

src/Providers/Maps/Google/Marker/Marker.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ namespace Provider.Maps.Google.Marker {
8585
// Let's return a promise that will be resolved or rejected according to the result
8686
return new Promise((resolve, reject) => {
8787
Helper.Conversions.ConvertToCoordinates(
88-
this.config.location,
89-
this.map.config.apiKey
88+
this.config.location
9089
)
9190
.then((response) => {
9291
markerOptions.position = {
@@ -234,8 +233,7 @@ namespace Provider.Maps.Google.Marker {
234233
switch (propValue) {
235234
case OSFramework.Maps.Enum.OS_Config_Marker.location:
236235
Helper.Conversions.ConvertToCoordinates(
237-
propertyValue as string,
238-
this.map.config.apiKey
236+
propertyValue as string
239237
)
240238
.then((response) => {
241239
this._provider.setPosition({

src/Providers/Maps/Google/Shape/AbstractPolyshape.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ namespace Provider.Maps.Google.Shape {
3232
return false;
3333
}
3434

35-
Helper.Conversions.ConvertToCoordinates(
36-
location,
37-
this.map.config.apiKey
38-
)
35+
Helper.Conversions.ConvertToCoordinates(location)
3936
.then((response) => {
4037
shapePath.set(index, {
4138
lat: response.lat,

src/Providers/Maps/Google/Shape/Circle.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ namespace Provider.Maps.Google.Shape {
3636
}
3737

3838
return new Promise((resolve, reject) => {
39-
Helper.Conversions.ConvertToCoordinates(
40-
location,
41-
this.map.config.apiKey
42-
)
39+
Helper.Conversions.ConvertToCoordinates(location)
4340
.then((response) => {
4441
const coordinates = {
4542
lat: response.lat,

src/Providers/Maps/Google/Shape/Rectangle.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ namespace Provider.Maps.Google.Shape {
6767
resolve(newBounds);
6868
}
6969
} else {
70-
Helper.Conversions.ConvertToCoordinates(
71-
bounds[cd],
72-
this.map.config.apiKey
73-
)
70+
Helper.Conversions.ConvertToCoordinates(bounds[cd])
7471
.then((response) => {
7572
boundsLength++;
7673
switch (cd) {

src/Providers/Maps/Google/SharedComponents/MapsAndSearchPlaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ namespace Provider.Maps.Google.SharedComponents {
9090
resolve(finalBounds);
9191
}
9292
} else {
93-
Helper.Conversions.ConvertToCoordinates(bounds[cd], apiKey)
93+
Helper.Conversions.ConvertToCoordinates(bounds[cd])
9494
.then((response) => {
9595
// Make sure to increment the boundsLength to validate whether the bounds structure has all the cardinal directions or not.
9696
boundsLength++;

0 commit comments

Comments
 (0)