PeliasGeocoderService.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /**
  46. * @function
  47. *
  48. * @param {String} query The query to be sent to the geocoder service
  49. * @param {GeocodeType} [type=GeocodeType.SEARCH] The type of geocode to perform.
  50. * @returns {Promise<GeocoderService.Result[]>}
  51. */
  52. PeliasGeocoderService.prototype.geocode = function (query, type) {
  53. //>>includeStart('debug', pragmas.debug);
  54. Check.typeOf.string("query", query);
  55. //>>includeEnd('debug');
  56. const resource = this._url.getDerivedResource({
  57. url: type === GeocodeType.AUTOCOMPLETE ? "autocomplete" : "search",
  58. queryParameters: {
  59. text: query,
  60. },
  61. });
  62. return resource.fetchJson().then(function (results) {
  63. return results.features.map(function (resultObject) {
  64. let destination;
  65. const bboxDegrees = resultObject.bbox;
  66. if (defined(bboxDegrees)) {
  67. destination = Rectangle.fromDegrees(
  68. bboxDegrees[0],
  69. bboxDegrees[1],
  70. bboxDegrees[2],
  71. bboxDegrees[3]
  72. );
  73. } else {
  74. const lon = resultObject.geometry.coordinates[0];
  75. const lat = resultObject.geometry.coordinates[1];
  76. destination = Cartesian3.fromDegrees(lon, lat);
  77. }
  78. return {
  79. displayName: resultObject.properties.label,
  80. destination: destination,
  81. };
  82. });
  83. });
  84. };
  85. export default PeliasGeocoderService;