OpenCageGeocoderService.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import Cartesian3 from "./Cartesian3.js";
  2. import Check from "./Check.js";
  3. import combine from "./combine.js";
  4. import defaultValue from "./defaultValue.js";
  5. import defined from "./defined.js";
  6. import Rectangle from "./Rectangle.js";
  7. import Resource from "./Resource.js";
  8. /**
  9. * Provides geocoding via a {@link https://opencagedata.com/|OpenCage} server.
  10. * @alias OpenCageGeocoderService
  11. * @constructor
  12. *
  13. * @param {Resource|String} url The endpoint to the OpenCage server.
  14. * @param {String} apiKey The OpenCage API Key.
  15. * @param {Object} [params] An object with the following properties (See https://opencagedata.com/api#forward-opt):
  16. * @param {Number} [params.abbrv] When set to 1 we attempt to abbreviate and shorten the formatted string we return.
  17. * @param {Number} [options.add_request] When set to 1 the various request parameters are added to the response for ease of debugging.
  18. * @param {String} [options.bounds] Provides the geocoder with a hint to the region that the query resides in.
  19. * @param {String} [options.countrycode] Restricts the results to the specified country or countries (as defined by the ISO 3166-1 Alpha 2 standard).
  20. * @param {String} [options.jsonp] Wraps the returned JSON with a function name.
  21. * @param {String} [options.language] An IETF format language code.
  22. * @param {Number} [options.limit] The maximum number of results we should return.
  23. * @param {Number} [options.min_confidence] An integer from 1-10. Only results with at least this confidence will be returned.
  24. * @param {Number} [options.no_annotations] When set to 1 results will not contain annotations.
  25. * @param {Number} [options.no_dedupe] When set to 1 results will not be deduplicated.
  26. * @param {Number} [options.no_record] When set to 1 the query contents are not logged.
  27. * @param {Number} [options.pretty] When set to 1 results are 'pretty' printed for easier reading. Useful for debugging.
  28. * @param {String} [options.proximity] Provides the geocoder with a hint to bias results in favour of those closer to the specified location (For example: 41.40139,2.12870).
  29. *
  30. * @example
  31. * // Configure a Viewer to use the OpenCage Geocoder
  32. * const viewer = new Cesium.Viewer('cesiumContainer', {
  33. * geocoder: new Cesium.OpenCageGeocoderService('https://api.opencagedata.com/geocode/v1/', '<API key>')
  34. * });
  35. */
  36. function OpenCageGeocoderService(url, apiKey, params) {
  37. //>>includeStart('debug', pragmas.debug);
  38. Check.defined("url", url);
  39. Check.defined("apiKey", apiKey);
  40. if (defined(params)) {
  41. Check.typeOf.object("params", params);
  42. }
  43. //>>includeEnd('debug');
  44. url = Resource.createIfNeeded(url);
  45. url.appendForwardSlash();
  46. url.setQueryParameters({ key: apiKey });
  47. this._url = url;
  48. this._params = defaultValue(params, {});
  49. }
  50. Object.defineProperties(OpenCageGeocoderService.prototype, {
  51. /**
  52. * The Resource used to access the OpenCage endpoint.
  53. * @type {Resource}
  54. * @memberof OpenCageGeocoderService.prototype
  55. * @readonly
  56. */
  57. url: {
  58. get: function () {
  59. return this._url;
  60. },
  61. },
  62. /**
  63. * Optional params passed to OpenCage in order to customize geocoding
  64. * @type {Object}
  65. * @memberof OpenCageGeocoderService.prototype
  66. * @readonly
  67. */
  68. params: {
  69. get: function () {
  70. return this._params;
  71. },
  72. },
  73. });
  74. /**
  75. * @function
  76. *
  77. * @param {String} query The query to be sent to the geocoder service
  78. * @returns {Promise<GeocoderService.Result[]>}
  79. */
  80. OpenCageGeocoderService.prototype.geocode = function (query) {
  81. //>>includeStart('debug', pragmas.debug);
  82. Check.typeOf.string("query", query);
  83. //>>includeEnd('debug');
  84. const resource = this._url.getDerivedResource({
  85. url: "json",
  86. queryParameters: combine(this._params, { q: query }),
  87. });
  88. return resource.fetchJson().then(function (response) {
  89. return response.results.map(function (resultObject) {
  90. let destination;
  91. const bounds = resultObject.bounds;
  92. if (defined(bounds)) {
  93. destination = Rectangle.fromDegrees(
  94. bounds.southwest.lng,
  95. bounds.southwest.lat,
  96. bounds.northeast.lng,
  97. bounds.northeast.lat
  98. );
  99. } else {
  100. const lon = resultObject.geometry.lat;
  101. const lat = resultObject.geometry.lng;
  102. destination = Cartesian3.fromDegrees(lon, lat);
  103. }
  104. return {
  105. displayName: resultObject.formatted,
  106. destination: destination,
  107. };
  108. });
  109. });
  110. };
  111. export default OpenCageGeocoderService;