PeliasGeocoderService.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import Cartesian3 from "./Cartesian3.js";
  2. import Check from "./Check.js";
  3. import defined from "./defined.js";
  4. import GeocodeType from "./GeocodeType.js";
  5. import Rectangle from "./Rectangle.js";
  6. import Resource from "./Resource.js";
  7. /**
  8. * Provides geocoding via a {@link https://pelias.io/|Pelias} server.
  9. * @alias PeliasGeocoderService
  10. * @constructor
  11. *
  12. * @param {Resource|string} url The endpoint to the Pelias server.
  13. *
  14. * @example
  15. * // Configure a Viewer to use the Pelias server hosted by https://geocode.earth/
  16. * const viewer = new Cesium.Viewer('cesiumContainer', {
  17. * geocoder: new Cesium.PeliasGeocoderService(new Cesium.Resource({
  18. * url: 'https://api.geocode.earth/v1/',
  19. * queryParameters: {
  20. * api_key: '<Your geocode.earth API key>'
  21. * }
  22. * }))
  23. * });
  24. */
  25. function PeliasGeocoderService(url) {
  26. //>>includeStart('debug', pragmas.debug);
  27. Check.defined("url", url);
  28. //>>includeEnd('debug');
  29. this._url = Resource.createIfNeeded(url);
  30. this._url.appendForwardSlash();
  31. }
  32. Object.defineProperties(PeliasGeocoderService.prototype, {
  33. /**
  34. * The Resource used to access the Pelias endpoint.
  35. * @type {Resource}
  36. * @memberof PeliasGeocoderService.prototype
  37. * @readonly
  38. */
  39. url: {
  40. get: function () {
  41. return this._url;
  42. },
  43. },
  44. /**
  45. * Gets the credit to display after a geocode is performed. Typically this is used to credit
  46. * the geocoder service.
  47. * @memberof PeliasGeocoderService.prototype
  48. * @type {Credit|undefined}
  49. * @readonly
  50. */
  51. credit: {
  52. get: function () {
  53. return undefined;
  54. },
  55. },
  56. });
  57. /**
  58. * @function
  59. *
  60. * @param {string} query The query to be sent to the geocoder service
  61. * @param {GeocodeType} [type=GeocodeType.SEARCH] The type of geocode to perform.
  62. * @returns {Promise<GeocoderService.Result[]>}
  63. */
  64. PeliasGeocoderService.prototype.geocode = async function (query, type) {
  65. //>>includeStart('debug', pragmas.debug);
  66. Check.typeOf.string("query", query);
  67. //>>includeEnd('debug');
  68. const resource = this._url.getDerivedResource({
  69. url: type === GeocodeType.AUTOCOMPLETE ? "autocomplete" : "search",
  70. queryParameters: {
  71. text: query,
  72. },
  73. });
  74. return resource.fetchJson().then(function (results) {
  75. return results.features.map(function (resultObject) {
  76. let destination;
  77. const bboxDegrees = resultObject.bbox;
  78. if (defined(bboxDegrees)) {
  79. destination = Rectangle.fromDegrees(
  80. bboxDegrees[0],
  81. bboxDegrees[1],
  82. bboxDegrees[2],
  83. bboxDegrees[3]
  84. );
  85. } else {
  86. const lon = resultObject.geometry.coordinates[0];
  87. const lat = resultObject.geometry.coordinates[1];
  88. destination = Cartesian3.fromDegrees(lon, lat);
  89. }
  90. return {
  91. displayName: resultObject.properties.label,
  92. destination: destination,
  93. attributions: results.attributions,
  94. };
  95. });
  96. });
  97. };
  98. export default PeliasGeocoderService;