@@ -34,12 +34,22 @@ export class GoogleAddressProvider implements AddressProvider {
34
34
return true ;
35
35
}
36
36
37
- public async getLocation ( data : GoogleAddressData ) : Promise < Cartographic > {
37
+ protected async getAuthRequestHeader ( ) : Promise < Record < string , string > > {
38
+ return { "X-Goog-Api-Key" : this . _apiKey } ;
39
+ }
40
+
41
+ protected async getPlacesBaseUrl ( ) : Promise < string > {
42
+ return "https://places.googleapis.com/v1/places/" ;
43
+ }
38
44
39
- const response = await fetch ( `https://places.googleapis.com/v1/places/${ data . placeId } ` , {
45
+ public async getLocation ( data : GoogleAddressData ) : Promise < Cartographic > {
46
+ let baseUrl = await this . getPlacesBaseUrl ( ) ;
47
+ const url = `${ baseUrl } ${ ! baseUrl . endsWith ( "/" ) ?"/" :"" } ${ data . placeId } ` ;
48
+ const authHeader = await this . getAuthRequestHeader ( ) ;
49
+ const response = await fetch ( url , {
40
50
method : "GET" ,
41
51
headers : {
42
- "X-Goog-Api-Key" : this . _apiKey ,
52
+ ... authHeader ,
43
53
"X-Goog-FieldMask" : "location" ,
44
54
} } ) ;
45
55
const json : any = await response . json ( ) ;
@@ -52,8 +62,13 @@ export class GoogleAddressProvider implements AddressProvider {
52
62
return Cartographic . fromDegrees ( { longitude : long , latitude : lat } ) ;
53
63
}
54
64
55
- protected getSuggestionsRequest ( query : string , viewRect : MapCartoRectangle ) : AddressRequest {
56
- const url = new URL ( "https://places.googleapis.com/v1/places:autocomplete" ) ;
65
+ protected async getPlacesAutoCompleteUrl ( ) : Promise < string > {
66
+ return "https://places.googleapis.com/v1/places:autocomplete" ;
67
+ }
68
+
69
+ protected async getSuggestionsRequest ( query : string , viewRect : MapCartoRectangle ) : Promise < AddressRequest > {
70
+ const urlStr = await this . getPlacesAutoCompleteUrl ( )
71
+ const url = new URL ( urlStr ) ;
57
72
58
73
const body = {
59
74
input : query ,
@@ -67,19 +82,20 @@ export class GoogleAddressProvider implements AddressProvider {
67
82
} ,
68
83
}
69
84
}
85
+ const authHeader = await this . getAuthRequestHeader ( ) ;
70
86
return {
71
87
url : url ,
72
88
method : "POST" ,
73
89
headers : {
74
- "X-Goog-Api-Key" : this . _apiKey ,
90
+ ... authHeader ,
75
91
"X-Goog-FieldMask" : "suggestions.placePrediction.text.text,suggestions.placePrediction.placeId" ,
76
92
} ,
77
93
body : JSON . stringify ( body ) ,
78
94
}
79
95
}
80
96
81
97
public async getSuggestions ( query : string , userView : MapCartoRectangle ) : Promise < AddressData [ ] > {
82
- const request = this . getSuggestionsRequest ( query , userView ) ;
98
+ const request = await this . getSuggestionsRequest ( query , userView ) ;
83
99
84
100
if ( query . length < 3 ) {
85
101
return [ ] ;
0 commit comments