UNPKG

@itwin/geo-tools-react

Version:
127 lines 5.67 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.GoogleAddressProvider = void 0; const core_common_1 = require("@itwin/core-common"); const core_frontend_1 = require("@itwin/core-frontend"); /** * Address provider for Google Places API. * It supports address suggestions and location retrieval based on place IDs. */ class GoogleAddressProvider { constructor(locationBiasRadius) { var _a, _b, _c, _d; this._radius = 5000; this.hasAddressIds = true; if (locationBiasRadius !== undefined) { this._radius = locationBiasRadius; } this._apiKey = ""; if ((_b = (_a = core_frontend_1.IModelApp.mapLayerFormatRegistry) === null || _a === void 0 ? void 0 : _a.configOptions) === null || _b === void 0 ? void 0 : _b.GoogleMaps) { this._apiKey = (_d = (_c = core_frontend_1.IModelApp.mapLayerFormatRegistry) === null || _c === void 0 ? void 0 : _c.configOptions) === null || _d === void 0 ? void 0 : _d.GoogleMaps.value; } } supportsAddressLocation() { return true; } isGoogleBaseMap(vp) { return ((vp === null || vp === void 0 ? void 0 : vp.viewFlags.backgroundMap) === true && (vp === null || vp === void 0 ? void 0 : vp.displayStyle.backgroundMapBase) instanceof core_common_1.BaseMapLayerSettings && vp.displayStyle.backgroundMapBase.formatId === "GoogleMaps"); } ; isDisabled(context) { return !this.isGoogleBaseMap(context.viewport); } ; async getAuthRequestHeader() { return { "X-Goog-Api-Key": this._apiKey }; } async getPlacesBaseUrl() { return "https://places.googleapis.com/v1/places/"; } async getLocation(data) { var _a, _b, _c, _d; let baseUrl = await this.getPlacesBaseUrl(); const url = `${baseUrl}${!baseUrl.endsWith("/") ? "/" : ""}${data.placeId}`; const authHeader = await this.getAuthRequestHeader(); const response = await fetch(url, { method: "GET", headers: { ...authHeader, "X-Goog-FieldMask": "location", } }); const json = await response.json(); const lat = (_b = (_a = json === null || json === void 0 ? void 0 : json.location) === null || _a === void 0 ? void 0 : _a.geometry) === null || _b === void 0 ? void 0 : _b.latitude; const long = (_d = (_c = json === null || json === void 0 ? void 0 : json.location) === null || _c === void 0 ? void 0 : _c.geometry) === null || _d === void 0 ? void 0 : _d.longitude; if (lat === undefined || long === undefined) { throw new Error("Invalid location data"); } return core_common_1.Cartographic.fromDegrees({ longitude: long, latitude: lat }); } async getPlacesAutoCompleteUrl() { return "https://places.googleapis.com/v1/places:autocomplete"; } async getSuggestionsRequest(query, viewRect) { const urlStr = await this.getPlacesAutoCompleteUrl(); const url = new URL(urlStr); const body = { input: query, locationBias: { circle: { center: { latitude: viewRect.cartoCenter.latitudeDegrees, longitude: viewRect.cartoCenter.longitudeDegrees, }, radius: this._radius, }, } }; const authHeader = await this.getAuthRequestHeader(); return { url: url, method: "POST", headers: { ...authHeader, "X-Goog-FieldMask": "suggestions.placePrediction.text.text,suggestions.placePrediction.placeId", }, body: JSON.stringify(body), }; } async getSuggestions(query, userView) { const request = await this.getSuggestionsRequest(query, userView); if (query.length < 3) { return []; } try { const response = await fetch(request.url, { method: request.method, headers: request.headers, body: request.body, }); const json = await response.json(); // Response format documented here: // https://developers.google.com/maps/documentation/places/web-service/place-autocomplete const addresses = []; if (Array.isArray(json.suggestions)) { json.suggestions.forEach((suggestion) => { var _a, _b, _c; addresses.push({ formattedAddress: (_b = (_a = suggestion === null || suggestion === void 0 ? void 0 : suggestion.placePrediction) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b.text, placeId: (_c = suggestion === null || suggestion === void 0 ? void 0 : suggestion.placePrediction) === null || _c === void 0 ? void 0 : _c.placeId }); }); } return addresses; } catch (error) { return []; } } } exports.GoogleAddressProvider = GoogleAddressProvider; //# sourceMappingURL=GoogleAddressProvider.js.map