IonGeocoderService.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import Check from "./Check.js";
  2. import Credit from "./Credit.js";
  3. import defaultValue from "./defaultValue.js";
  4. import defined from "./defined.js";
  5. import Ion from "./Ion.js";
  6. import PeliasGeocoderService from "./PeliasGeocoderService.js";
  7. import Resource from "./Resource.js";
  8. /**
  9. * Provides geocoding through Cesium ion.
  10. * @alias IonGeocoderService
  11. * @constructor
  12. *
  13. * @param {object} options Object with the following properties:
  14. * @param {Scene} options.scene The scene
  15. * @param {string} [options.accessToken=Ion.defaultAccessToken] The access token to use.
  16. * @param {string|Resource} [options.server=Ion.defaultServer] The resource to the Cesium ion API server.
  17. *
  18. * @see Ion
  19. */
  20. function IonGeocoderService(options) {
  21. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  22. //>>includeStart('debug', pragmas.debug);
  23. Check.typeOf.object("options.scene", options.scene);
  24. //>>includeEnd('debug');
  25. const accessToken = defaultValue(options.accessToken, Ion.defaultAccessToken);
  26. const server = Resource.createIfNeeded(
  27. defaultValue(options.server, Ion.defaultServer)
  28. );
  29. server.appendForwardSlash();
  30. const defaultTokenCredit = Ion.getDefaultTokenCredit(accessToken);
  31. if (defined(defaultTokenCredit)) {
  32. options.scene.frameState.creditDisplay.addStaticCredit(
  33. Credit.clone(defaultTokenCredit)
  34. );
  35. }
  36. const searchEndpoint = server.getDerivedResource({
  37. url: "v1/geocode",
  38. });
  39. if (defined(accessToken)) {
  40. searchEndpoint.appendQueryParameters({ access_token: accessToken });
  41. }
  42. this._accessToken = accessToken;
  43. this._server = server;
  44. this._pelias = new PeliasGeocoderService(searchEndpoint);
  45. }
  46. Object.defineProperties(IonGeocoderService.prototype, {
  47. /**
  48. * Gets the credit to display after a geocode is performed. Typically this is used to credit
  49. * the geocoder service.
  50. * @memberof IonGeocoderService.prototype
  51. * @type {Credit|undefined}
  52. * @readonly
  53. */
  54. credit: {
  55. get: function () {
  56. return undefined;
  57. },
  58. },
  59. });
  60. /**
  61. * @function
  62. *
  63. * @param {string} query The query to be sent to the geocoder service
  64. * @param {GeocodeType} [type=GeocodeType.SEARCH] The type of geocode to perform.
  65. * @returns {Promise<GeocoderService.Result[]>}
  66. */
  67. IonGeocoderService.prototype.geocode = async function (query, geocodeType) {
  68. return this._pelias.geocode(query, geocodeType);
  69. };
  70. export default IonGeocoderService;