Geocoder.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*global define*/
  2. define([
  3. '../../Core/defined',
  4. '../../Core/defineProperties',
  5. '../../Core/destroyObject',
  6. '../../Core/DeveloperError',
  7. '../../ThirdParty/knockout',
  8. '../getElement',
  9. './GeocoderViewModel'
  10. ], function(
  11. defined,
  12. defineProperties,
  13. destroyObject,
  14. DeveloperError,
  15. knockout,
  16. getElement,
  17. GeocoderViewModel) {
  18. "use strict";
  19. var startSearchPath = 'M29.772,26.433l-7.126-7.126c0.96-1.583,1.523-3.435,1.524-5.421C24.169,8.093,19.478,3.401,13.688,3.399C7.897,3.401,3.204,8.093,3.204,13.885c0,5.789,4.693,10.481,10.484,10.481c1.987,0,3.839-0.563,5.422-1.523l7.128,7.127L29.772,26.433zM7.203,13.885c0.006-3.582,2.903-6.478,6.484-6.486c3.579,0.008,6.478,2.904,6.484,6.486c-0.007,3.58-2.905,6.476-6.484,6.484C10.106,20.361,7.209,17.465,7.203,13.885z';
  20. var stopSearchPath = 'M24.778,21.419 19.276,15.917 24.777,10.415 21.949,7.585 16.447,13.087 10.945,7.585 8.117,10.415 13.618,15.917 8.116,21.419 10.946,24.248 16.447,18.746 21.948,24.248z';
  21. /**
  22. * A widget for finding addresses and landmarks, and flying the camera to them. Geocoding is
  23. * performed using the {@link http://msdn.microsoft.com/en-us/library/ff701715.aspx|Bing Maps Locations API}.
  24. *
  25. * @alias Geocoder
  26. * @constructor
  27. *
  28. * @param {Object} options Object with the following properties:
  29. * @param {Element|String} options.container The DOM element or ID that will contain the widget.
  30. * @param {Scene} options.scene The Scene instance to use.
  31. * @param {String} [options.url='//dev.virtualearth.net'] The base URL of the Bing Maps API.
  32. * @param {String} [options.key] The Bing Maps key for your application, which can be
  33. * created at {@link https://www.bingmapsportal.com}.
  34. * If this parameter is not provided, {@link BingMapsApi.defaultKey} is used.
  35. * If {@link BingMapsApi.defaultKey} is undefined as well, a message is
  36. * written to the console reminding you that you must create and supply a Bing Maps
  37. * key as soon as possible. Please do not deploy an application that uses
  38. * this widget without creating a separate key for your application.
  39. * @param {Number} [options.flightDuration=1.5] The duration of the camera flight to an entered location, in seconds.
  40. */
  41. var Geocoder = function(options) {
  42. //>>includeStart('debug', pragmas.debug);
  43. if (!defined(options) || !defined(options.container)) {
  44. throw new DeveloperError('options.container is required.');
  45. }
  46. if (!defined(options.scene)) {
  47. throw new DeveloperError('options.scene is required.');
  48. }
  49. //>>includeEnd('debug');
  50. var container = getElement(options.container);
  51. var viewModel = new GeocoderViewModel(options);
  52. viewModel._startSearchPath = startSearchPath;
  53. viewModel._stopSearchPath = stopSearchPath;
  54. var form = document.createElement('form');
  55. form.setAttribute('data-bind', 'submit: search');
  56. var textBox = document.createElement('input');
  57. textBox.type = 'search';
  58. textBox.className = 'cesium-geocoder-input';
  59. textBox.setAttribute('placeholder', 'Enter an address or landmark...');
  60. textBox.setAttribute('data-bind', '\
  61. value: searchText,\
  62. valueUpdate: "afterkeydown",\
  63. disable: isSearchInProgress,\
  64. css: { "cesium-geocoder-input-wide" : searchText.length > 0 }');
  65. form.appendChild(textBox);
  66. var searchButton = document.createElement('span');
  67. searchButton.className = 'cesium-geocoder-searchButton';
  68. searchButton.setAttribute('data-bind', '\
  69. click: search,\
  70. cesiumSvgPath: { path: isSearchInProgress ? _stopSearchPath : _startSearchPath, width: 32, height: 32 }');
  71. form.appendChild(searchButton);
  72. container.appendChild(form);
  73. knockout.applyBindings(viewModel, form);
  74. this._container = container;
  75. this._viewModel = viewModel;
  76. this._form = form;
  77. this._onInputBegin = function(e) {
  78. if (!container.contains(e.target)) {
  79. textBox.blur();
  80. }
  81. };
  82. this._onInputEnd = function(e) {
  83. if (container.contains(e.target)) {
  84. textBox.focus();
  85. }
  86. };
  87. //We subscribe to both begin and end events in order to give the text box
  88. //focus no matter where on the widget is clicked.
  89. document.addEventListener('mousedown', this._onInputBegin, true);
  90. document.addEventListener('mouseup', this._onInputEnd, true);
  91. document.addEventListener('touchstart', this._onInputBegin, true);
  92. document.addEventListener('touchend', this._onInputEnd, true);
  93. };
  94. defineProperties(Geocoder.prototype, {
  95. /**
  96. * Gets the parent container.
  97. * @memberof Geocoder.prototype
  98. *
  99. * @type {Element}
  100. */
  101. container : {
  102. get : function() {
  103. return this._container;
  104. }
  105. },
  106. /**
  107. * Gets the view model.
  108. * @memberof Geocoder.prototype
  109. *
  110. * @type {GeocoderViewModel}
  111. */
  112. viewModel : {
  113. get : function() {
  114. return this._viewModel;
  115. }
  116. }
  117. });
  118. /**
  119. * @returns {Boolean} true if the object has been destroyed, false otherwise.
  120. */
  121. Geocoder.prototype.isDestroyed = function() {
  122. return false;
  123. };
  124. /**
  125. * Destroys the widget. Should be called if permanently
  126. * removing the widget from layout.
  127. */
  128. Geocoder.prototype.destroy = function() {
  129. document.removeEventListener('mousedown', this._onInputBegin, true);
  130. document.removeEventListener('mouseup', this._onInputEnd, true);
  131. document.removeEventListener('touchstart', this._onInputBegin, true);
  132. document.removeEventListener('touchend', this._onInputEnd, true);
  133. knockout.cleanNode(this._form);
  134. this._container.removeChild(this._form);
  135. return destroyObject(this);
  136. };
  137. return Geocoder;
  138. });