@itwin/geo-tools-react
Version:
React Geospatial Tools
64 lines • 2.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BingAddressProvider = void 0;
const core_frontend_1 = require("@itwin/core-frontend");
class BingAddressProvider {
supportsAddressLocation() {
return false;
}
isDisabled(_context) {
return false;
}
;
constructor(radius, maxResults, entityTypes) {
var _a, _b;
this._radius = 5000;
this._maxResults = 10;
this._entityTypes = ["Address,Place"];
this.hasAddressIds = false;
if (maxResults !== undefined) {
this._maxResults = maxResults;
}
if (radius !== undefined) {
this._radius = radius;
}
if (entityTypes && entityTypes.length > 0) {
this._entityTypes = entityTypes;
}
this._bingKey = "";
if ((_b = (_a = core_frontend_1.IModelApp.mapLayerFormatRegistry) === null || _a === void 0 ? void 0 : _a.configOptions) === null || _b === void 0 ? void 0 : _b.BingMaps) {
this._bingKey = core_frontend_1.IModelApp.mapLayerFormatRegistry.configOptions.BingMaps.value;
}
}
// Sample request: http://dev.virtualearth.net/REST/v1/Autosuggest?query=<user_prefix>&userLocation=<lat,long,confidence_radius>&userCircularMapView=<lat,long,radius>&userMapView=<nw_lat,nw_long,se_lat,se_long>&maxResults=<max_results>&includeEntityTypes=<Place,Address,Business>&culture=<culture_code>&userRegion=<country_code>&countryFilter=<country_code_or_none>&key=<BingMapKey>
getSuggestionsRequest(query, viewRect) {
const entityTypesStr = this._entityTypes.join();
const userCircularMapView = `${viewRect.cartoCenter.latitudeDegrees}, ${viewRect.cartoCenter.longitudeDegrees}, ${this._radius}`;
return {
method: "GET",
url: new URL(`https://dev.virtualearth.net/REST/v1/Autosuggest?query=${query}&userCircularMapView=${userCircularMapView}&maxResults=${this._maxResults}&includeEntityTypes=${entityTypesStr}&key=${this._bingKey}`),
};
}
async getSuggestions(query, viewRect) {
const request = this.getSuggestionsRequest(query, viewRect);
try {
const response = await fetch(request.url, { method: "GET" });
const json = await response.json();
// Response format documented here:
// https://docs.microsoft.com/en-us/bingmaps/rest-services/autosuggest#response-format
const value = json.resourceSets[0].resources[0].value;
const addresses = [];
if (Array.isArray(value)) {
value.forEach((address) => {
addresses.push({ formattedAddress: address.address.formattedAddress });
});
}
return addresses;
}
catch (error) {
return [];
}
}
}
exports.BingAddressProvider = BingAddressProvider;
//# sourceMappingURL=BingAddressProvider.js.map