BingMapsGeocoderService.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import Check from "./Check.js";
  2. import Credit from "./Credit.js";
  3. import defaultValue from "./defaultValue.js";
  4. import Rectangle from "./Rectangle.js";
  5. import Resource from "./Resource.js";
  6. import defined from "./defined.js";
  7. import DeveloperError from "./DeveloperError.js";
  8. const url = "https://dev.virtualearth.net/REST/v1/Locations";
  9. /**
  10. * Provides geocoding through Bing Maps.
  11. * @alias BingMapsGeocoderService
  12. * @constructor
  13. *
  14. * @param {object} options Object with the following properties:
  15. * @param {string} options.key A key to use with the Bing Maps geocoding service
  16. * @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.
  17. */
  18. function BingMapsGeocoderService(options) {
  19. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  20. const key = options.key;
  21. //>>includeStart('debug', pragmas.debug);
  22. if (!defined(key)) {
  23. throw new DeveloperError("options.key is required.");
  24. }
  25. //>>includeEnd('debug');
  26. this._key = key;
  27. const queryParameters = {
  28. key: key,
  29. };
  30. if (defined(options.culture)) {
  31. queryParameters.culture = options.culture;
  32. }
  33. this._resource = new Resource({
  34. url: url,
  35. queryParameters: queryParameters,
  36. });
  37. this._credit = new Credit(
  38. `<img src="http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png"\/>`,
  39. false
  40. );
  41. }
  42. Object.defineProperties(BingMapsGeocoderService.prototype, {
  43. /**
  44. * The URL endpoint for the Bing geocoder service
  45. * @type {string}
  46. * @memberof BingMapsGeocoderService.prototype
  47. * @readonly
  48. */
  49. url: {
  50. get: function () {
  51. return url;
  52. },
  53. },
  54. /**
  55. * The key for the Bing geocoder service
  56. * @type {string}
  57. * @memberof BingMapsGeocoderService.prototype
  58. * @readonly
  59. */
  60. key: {
  61. get: function () {
  62. return this._key;
  63. },
  64. },
  65. /**
  66. * Gets the credit to display after a geocode is performed. Typically this is used to credit
  67. * the geocoder service.
  68. * @memberof BingMapsGeocoderService.prototype
  69. * @type {Credit|undefined}
  70. * @readonly
  71. */
  72. credit: {
  73. get: function () {
  74. return this._credit;
  75. },
  76. },
  77. });
  78. /**
  79. * @function
  80. *
  81. * @param {string} query The query to be sent to the geocoder service
  82. * @returns {Promise<GeocoderService.Result[]>}
  83. */
  84. BingMapsGeocoderService.prototype.geocode = async function (query) {
  85. //>>includeStart('debug', pragmas.debug);
  86. Check.typeOf.string("query", query);
  87. //>>includeEnd('debug');
  88. const resource = this._resource.getDerivedResource({
  89. queryParameters: {
  90. query: query,
  91. },
  92. });
  93. return resource.fetchJsonp("jsonp").then(function (result) {
  94. if (result.resourceSets.length === 0) {
  95. return [];
  96. }
  97. const results = result.resourceSets[0].resources;
  98. return results.map(function (resource) {
  99. const bbox = resource.bbox;
  100. const south = bbox[0];
  101. const west = bbox[1];
  102. const north = bbox[2];
  103. const east = bbox[3];
  104. return {
  105. displayName: resource.name,
  106. destination: Rectangle.fromDegrees(west, south, east, north),
  107. };
  108. });
  109. });
  110. };
  111. export default BingMapsGeocoderService;