OpenCageGeocoderService.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 defineProperties from './defineProperties.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. * var 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. }
  51. defineProperties(OpenCageGeocoderService.prototype, {
  52. /**
  53. * The Resource used to access the OpenCage endpoint.
  54. * @type {Resource}
  55. * @memberof {OpenCageGeocoderService.prototype}
  56. * @readonly
  57. */
  58. url: {
  59. get: function () {
  60. return this._url;
  61. }
  62. },
  63. /**
  64. * Optional params passed to OpenCage in order to customize geocoding
  65. * @type {Object}
  66. * @memberof {OpenCageGeocoderService.prototype}
  67. * @readonly
  68. */
  69. params: {
  70. get: function () {
  71. return this._params;
  72. }
  73. }
  74. });
  75. /**
  76. * @function
  77. *
  78. * @param {String} query The query to be sent to the geocoder service
  79. * @returns {Promise<GeocoderService~Result[]>}
  80. */
  81. OpenCageGeocoderService.prototype.geocode = function(query) {
  82. //>>includeStart('debug', pragmas.debug);
  83. Check.typeOf.string('query', query);
  84. //>>includeEnd('debug');
  85. var resource = this._url.getDerivedResource({
  86. url: 'json',
  87. queryParameters: combine(this._params, {q: query})
  88. });
  89. return resource.fetchJson()
  90. .then(function (response) {
  91. return response.results.map(function (resultObject) {
  92. var destination;
  93. var bounds = resultObject.bounds;
  94. if (defined(bounds)) {
  95. destination = Rectangle.fromDegrees(bounds.southwest.lng, bounds.southwest.lat, bounds.northeast.lng, bounds.northeast.lat);
  96. } else {
  97. var lon = resultObject.geometry.lat;
  98. var lat = resultObject.geometry.lng;
  99. destination = Cartesian3.fromDegrees(lon, lat);
  100. }
  101. return {
  102. displayName: resultObject.formatted,
  103. destination: destination
  104. };
  105. });
  106. });
  107. };
  108. export default OpenCageGeocoderService;