PeliasGeocoderService.js 3.1 KB

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