OpenCageGeocoderService.js 4.8 KB

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