@@ -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}
0 commit comments