BingMapsGeocoderService.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import BingMapsApi from "./BingMapsApi.js";
  2. import Check from "./Check.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. var 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. */
  17. function BingMapsGeocoderService(options) {
  18. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  19. var key = BingMapsApi._getKeyNoDeprecate(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. this._resource = new Resource({
  27. url: url,
  28. queryParameters: {
  29. key: key,
  30. },
  31. });
  32. }
  33. Object.defineProperties(BingMapsGeocoderService.prototype, {
  34. /**
  35. * The URL endpoint for the Bing geocoder service
  36. * @type {String}
  37. * @memberof BingMapsGeocoderService.prototype
  38. * @readonly
  39. */
  40. url: {
  41. get: function () {
  42. return url;
  43. },
  44. },
  45. /**
  46. * The key for the Bing geocoder service
  47. * @type {String}
  48. * @memberof BingMapsGeocoderService.prototype
  49. * @readonly
  50. */
  51. key: {
  52. get: function () {
  53. return this._key;
  54. },
  55. },
  56. });
  57. /**
  58. * @function
  59. *
  60. * @param {String} query The query to be sent to the geocoder service
  61. * @returns {Promise<GeocoderService.Result[]>}
  62. */
  63. BingMapsGeocoderService.prototype.geocode = function (query) {
  64. //>>includeStart('debug', pragmas.debug);
  65. Check.typeOf.string("query", query);
  66. //>>includeEnd('debug');
  67. var resource = this._resource.getDerivedResource({
  68. queryParameters: {
  69. query: query,
  70. },
  71. });
  72. return resource.fetchJsonp("jsonp").then(function (result) {
  73. if (result.resourceSets.length === 0) {
  74. return [];
  75. }
  76. var results = result.resourceSets[0].resources;
  77. return results.map(function (resource) {
  78. var bbox = resource.bbox;
  79. var south = bbox[0];
  80. var west = bbox[1];
  81. var north = bbox[2];
  82. var east = bbox[3];
  83. return {
  84. displayName: resource.name,
  85. destination: Rectangle.fromDegrees(west, south, east, north),
  86. };
  87. });
  88. });
  89. };
  90. export default BingMapsGeocoderService;