BingMapsGeocoderService.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import Check from "./Check.js";
  2. import defaultValue from "./defaultValue.js";
  3. import Rectangle from "./Rectangle.js";
  4. import Resource from "./Resource.js";
  5. import defined from "./defined.js";
  6. import DeveloperError from "./DeveloperError.js";
  7. const url = "https://dev.virtualearth.net/REST/v1/Locations";
  8. /**
  9. * Provides geocoding through Bing Maps.
  10. * @alias BingMapsGeocoderService
  11. * @constructor
  12. *
  13. * @param {Object} options Object with the following properties:
  14. * @param {String} options.key A key to use with the Bing Maps geocoding service
  15. * @param {String} [options.culture] A Bing Maps {@link https://docs.microsoft.com/en-us/bingmaps/rest-services/common-parameters-and-types/supported-culture-codes|Culture Code} to return results in a specific culture and language.
  16. */
  17. function BingMapsGeocoderService(options) {
  18. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  19. const key = options.key;
  20. //>>includeStart('debug', pragmas.debug);
  21. if (!defined(key)) {
  22. throw new DeveloperError("options.key is required.");
  23. }
  24. //>>includeEnd('debug');
  25. this._key = key;
  26. const queryParameters = {
  27. key: key,
  28. };
  29. if (defined(options.culture)) {
  30. queryParameters.culture = options.culture;
  31. }
  32. this._resource = new Resource({
  33. url: url,
  34. queryParameters: queryParameters,
  35. });
  36. }
  37. Object.defineProperties(BingMapsGeocoderService.prototype, {
  38. /**
  39. * The URL endpoint for the Bing geocoder service
  40. * @type {String}
  41. * @memberof BingMapsGeocoderService.prototype
  42. * @readonly
  43. */
  44. url: {
  45. get: function () {
  46. return url;
  47. },
  48. },
  49. /**
  50. * The key for the Bing geocoder service
  51. * @type {String}
  52. * @memberof BingMapsGeocoderService.prototype
  53. * @readonly
  54. */
  55. key: {
  56. get: function () {
  57. return this._key;
  58. },
  59. },
  60. });
  61. /**
  62. * @function
  63. *
  64. * @param {String} query The query to be sent to the geocoder service
  65. * @returns {Promise<GeocoderService.Result[]>}
  66. */
  67. BingMapsGeocoderService.prototype.geocode = function (query) {
  68. //>>includeStart('debug', pragmas.debug);
  69. Check.typeOf.string("query", query);
  70. //>>includeEnd('debug');
  71. const resource = this._resource.getDerivedResource({
  72. queryParameters: {
  73. query: query,
  74. },
  75. });
  76. return resource.fetchJsonp("jsonp").then(function (result) {
  77. if (result.resourceSets.length === 0) {
  78. return [];
  79. }
  80. const results = result.resourceSets[0].resources;
  81. return results.map(function (resource) {
  82. const bbox = resource.bbox;
  83. const south = bbox[0];
  84. const west = bbox[1];
  85. const north = bbox[2];
  86. const east = bbox[3];
  87. return {
  88. displayName: resource.name,
  89. destination: Rectangle.fromDegrees(west, south, east, north),
  90. };
  91. });
  92. });
  93. };
  94. export default BingMapsGeocoderService;