Geocoder.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import defined from "../../Core/defined.js";
  2. import destroyObject from "../../Core/destroyObject.js";
  3. import DeveloperError from "../../Core/DeveloperError.js";
  4. import FeatureDetection from "../../Core/FeatureDetection.js";
  5. import knockout from "../../ThirdParty/knockout.js";
  6. import getElement from "../getElement.js";
  7. import GeocoderViewModel from "./GeocoderViewModel.js";
  8. const startSearchPath =
  9. "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";
  10. const stopSearchPath =
  11. "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";
  12. /**
  13. * A widget for finding addresses and landmarks, and flying the camera to them. Geocoding is
  14. * performed using {@link https://cesium.com/cesium-ion/|Cesium ion}.
  15. *
  16. * @alias Geocoder
  17. * @constructor
  18. *
  19. * @param {Object} options Object with the following properties:
  20. * @param {Element|String} options.container The DOM element or ID that will contain the widget.
  21. * @param {Scene} options.scene The Scene instance to use.
  22. * @param {GeocoderService[]} [options.geocoderServices] The geocoder services to be used
  23. * @param {Boolean} [options.autoComplete = true] True if the geocoder should query as the user types to autocomplete
  24. * @param {Number} [options.flightDuration=1.5] The duration of the camera flight to an entered location, in seconds.
  25. * @param {Geocoder.DestinationFoundFunction} [options.destinationFound=GeocoderViewModel.flyToDestination] A callback function that is called after a successful geocode. If not supplied, the default behavior is to fly the camera to the result destination.
  26. */
  27. function Geocoder(options) {
  28. //>>includeStart('debug', pragmas.debug);
  29. if (!defined(options) || !defined(options.container)) {
  30. throw new DeveloperError("options.container is required.");
  31. }
  32. if (!defined(options.scene)) {
  33. throw new DeveloperError("options.scene is required.");
  34. }
  35. //>>includeEnd('debug');
  36. const container = getElement(options.container);
  37. const viewModel = new GeocoderViewModel(options);
  38. viewModel._startSearchPath = startSearchPath;
  39. viewModel._stopSearchPath = stopSearchPath;
  40. const form = document.createElement("form");
  41. form.setAttribute("data-bind", "submit: search");
  42. const textBox = document.createElement("input");
  43. textBox.type = "search";
  44. textBox.className = "cesium-geocoder-input";
  45. textBox.setAttribute("placeholder", "Enter an address or landmark...");
  46. textBox.setAttribute(
  47. "data-bind",
  48. '\
  49. textInput: searchText,\
  50. disable: isSearchInProgress,\
  51. event: { keyup: handleKeyUp, keydown: handleKeyDown, mouseover: deselectSuggestion },\
  52. css: { "cesium-geocoder-input-wide" : keepExpanded || searchText.length > 0 },\
  53. hasFocus: _focusTextbox'
  54. );
  55. this._onTextBoxFocus = function () {
  56. // as of 2016-10-19, setTimeout is required to ensure that the
  57. // text is focused on Safari 10
  58. setTimeout(function () {
  59. textBox.select();
  60. }, 0);
  61. };
  62. textBox.addEventListener("focus", this._onTextBoxFocus, false);
  63. form.appendChild(textBox);
  64. this._textBox = textBox;
  65. const searchButton = document.createElement("span");
  66. searchButton.className = "cesium-geocoder-searchButton";
  67. searchButton.setAttribute(
  68. "data-bind",
  69. "\
  70. click: search,\
  71. cesiumSvgPath: { path: isSearchInProgress ? _stopSearchPath : _startSearchPath, width: 32, height: 32 }"
  72. );
  73. form.appendChild(searchButton);
  74. container.appendChild(form);
  75. const searchSuggestionsContainer = document.createElement("div");
  76. searchSuggestionsContainer.className = "search-results";
  77. searchSuggestionsContainer.setAttribute(
  78. "data-bind",
  79. "visible: _suggestionsVisible"
  80. );
  81. const suggestionsList = document.createElement("ul");
  82. suggestionsList.setAttribute("data-bind", "foreach: _suggestions");
  83. const suggestions = document.createElement("li");
  84. suggestionsList.appendChild(suggestions);
  85. suggestions.setAttribute(
  86. "data-bind",
  87. "text: $data.displayName, \
  88. click: $parent.activateSuggestion, \
  89. event: { mouseover: $parent.handleMouseover}, \
  90. css: { active: $data === $parent._selectedSuggestion }"
  91. );
  92. searchSuggestionsContainer.appendChild(suggestionsList);
  93. container.appendChild(searchSuggestionsContainer);
  94. knockout.applyBindings(viewModel, form);
  95. knockout.applyBindings(viewModel, searchSuggestionsContainer);
  96. this._container = container;
  97. this._searchSuggestionsContainer = searchSuggestionsContainer;
  98. this._viewModel = viewModel;
  99. this._form = form;
  100. this._onInputBegin = function (e) {
  101. // e.target will not be correct if we are inside of the Shadow DOM
  102. // and contains will always fail. To retrieve the correct target,
  103. // we need to access the first element of the composedPath.
  104. // This allows us to use shadow DOM if it exists and fall
  105. // back to legacy behavior if its not being used.
  106. let target = e.target;
  107. if (typeof e.composedPath === "function") {
  108. target = e.composedPath()[0];
  109. }
  110. if (!container.contains(target)) {
  111. viewModel._focusTextbox = false;
  112. viewModel.hideSuggestions();
  113. }
  114. };
  115. this._onInputEnd = function (e) {
  116. viewModel._focusTextbox = true;
  117. viewModel.showSuggestions();
  118. };
  119. //We subscribe to both begin and end events in order to give the text box
  120. //focus no matter where on the widget is clicked.
  121. if (FeatureDetection.supportsPointerEvents()) {
  122. document.addEventListener("pointerdown", this._onInputBegin, true);
  123. container.addEventListener("pointerup", this._onInputEnd, true);
  124. container.addEventListener("pointercancel", this._onInputEnd, true);
  125. } else {
  126. document.addEventListener("mousedown", this._onInputBegin, true);
  127. container.addEventListener("mouseup", this._onInputEnd, true);
  128. document.addEventListener("touchstart", this._onInputBegin, true);
  129. container.addEventListener("touchend", this._onInputEnd, true);
  130. container.addEventListener("touchcancel", this._onInputEnd, true);
  131. }
  132. }
  133. Object.defineProperties(Geocoder.prototype, {
  134. /**
  135. * Gets the parent container.
  136. * @memberof Geocoder.prototype
  137. *
  138. * @type {Element}
  139. */
  140. container: {
  141. get: function () {
  142. return this._container;
  143. },
  144. },
  145. /**
  146. * Gets the parent container.
  147. * @memberof Geocoder.prototype
  148. *
  149. * @type {Element}
  150. */
  151. searchSuggestionsContainer: {
  152. get: function () {
  153. return this._searchSuggestionsContainer;
  154. },
  155. },
  156. /**
  157. * Gets the view model.
  158. * @memberof Geocoder.prototype
  159. *
  160. * @type {GeocoderViewModel}
  161. */
  162. viewModel: {
  163. get: function () {
  164. return this._viewModel;
  165. },
  166. },
  167. });
  168. /**
  169. * @returns {Boolean} true if the object has been destroyed, false otherwise.
  170. */
  171. Geocoder.prototype.isDestroyed = function () {
  172. return false;
  173. };
  174. /**
  175. * Destroys the widget. Should be called if permanently
  176. * removing the widget from layout.
  177. */
  178. Geocoder.prototype.destroy = function () {
  179. const container = this._container;
  180. if (FeatureDetection.supportsPointerEvents()) {
  181. document.removeEventListener("pointerdown", this._onInputBegin, true);
  182. container.removeEventListener("pointerup", this._onInputEnd, true);
  183. } else {
  184. document.removeEventListener("mousedown", this._onInputBegin, true);
  185. container.removeEventListener("mouseup", this._onInputEnd, true);
  186. document.removeEventListener("touchstart", this._onInputBegin, true);
  187. container.removeEventListener("touchend", this._onInputEnd, true);
  188. }
  189. this._viewModel.destroy();
  190. knockout.cleanNode(this._form);
  191. knockout.cleanNode(this._searchSuggestionsContainer);
  192. container.removeChild(this._form);
  193. container.removeChild(this._searchSuggestionsContainer);
  194. this._textBox.removeEventListener("focus", this._onTextBoxFocus, false);
  195. return destroyObject(this);
  196. };
  197. /**
  198. * A function that handles the result of a successful geocode.
  199. * @callback Geocoder.DestinationFoundFunction
  200. * @param {GeocoderViewModel} viewModel The view model.
  201. * @param {Cartesian3|Rectangle} destination The destination result of the geocode.
  202. */
  203. export default Geocoder;