Skip to content

Commit 81f601d

Browse files
wip
1 parent 7269f71 commit 81f601d

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

packages/itwin/geo-tools/src/GoogleAddressProvider.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,22 @@ export class GoogleAddressProvider implements AddressProvider {
3434
return true;
3535
}
3636

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+
}
3844

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, {
4050
method: "GET",
4151
headers: {
42-
"X-Goog-Api-Key": this._apiKey,
52+
...authHeader,
4353
"X-Goog-FieldMask": "location",
4454
} });
4555
const json: any = await response.json();
@@ -52,8 +62,13 @@ export class GoogleAddressProvider implements AddressProvider {
5262
return Cartographic.fromDegrees({longitude: long, latitude: lat});
5363
}
5464

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);
5772

5873
const body = {
5974
input: query,
@@ -67,19 +82,20 @@ export class GoogleAddressProvider implements AddressProvider {
6782
},
6883
}
6984
}
85+
const authHeader = await this.getAuthRequestHeader();
7086
return {
7187
url: url,
7288
method: "POST",
7389
headers: {
74-
"X-Goog-Api-Key": this._apiKey,
90+
...authHeader,
7591
"X-Goog-FieldMask": "suggestions.placePrediction.text.text,suggestions.placePrediction.placeId",
7692
},
7793
body: JSON.stringify(body),
7894
}
7995
}
8096

8197
public async getSuggestions(query: string, userView: MapCartoRectangle): Promise<AddressData[]> {
82-
const request = this.getSuggestionsRequest(query, userView);
98+
const request = await this.getSuggestionsRequest(query, userView);
8399

84100
if (query.length < 3) {
85101
return [];

0 commit comments

Comments
 (0)