Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. /** api: xtype = gxp_googlegeocodercombo */
  2. xtype: "gxp_googlegeocodercombo",
  3.  
  4. /** api: config[queryDelay]
  5. * ``Number`` Delay before the search occurs. Default is 100ms.
  6. */
  7. queryDelay: 100,
  8.  
  9. /** api: config[bounds]
  10. * ``OpenLayers.Bounds | Array`` Optional bounds (in geographic coordinates)
  11. * for restricting search.
  12. */
  13.  
  14. /** api: config[valueField]
  15. * ``String``
  16. * Field from selected record to use when the combo's ``getValue`` method
  17. * is called. Default is "location". Possible value's are "location",
  18. * "viewport", or "address". The location value will be an
  19. * ``OpenLayers.LonLat`` object that corresponds to the geocoded address.
  20. * The viewport value will be an ``OpenLayers.Bounds`` object that is
  21. * the recommended viewport for viewing the resulting location. The
  22. * address value will be a string that is the formatted address.
  23. */
  24. valueField: "viewport",
  25.  
  26. /** private: config[displayField]
  27. */
  28. displayField: "address",
  29.  
  30. /** private: method[initComponent]
  31. * Override
  32. */
  33. initComponent: function() {
  34.  
  35. // only enable when Google Maps API is available
  36. this.disabled = true;
  37. var ready = !!(window.google && google.maps);
  38. if (!ready) {
  39. if (!gxp.plugins || !gxp.plugins.GoogleSource) {
  40. throw new Error("The gxp.form.GoogleGeocoderComboBox requires the gxp.plugins.GoogleSource or the Google Maps V3 API to be loaded.");
  41. }
  42. gxp.plugins.GoogleSource.loader.onLoad({
  43. otherParams: gxp.plugins.GoogleSource.prototype.otherParams,
  44. callback: this.prepGeocoder,
  45. errback: function() {
  46. throw new Error("The Google Maps script failed to load within the given timeout.");
  47. },
  48. scope: this
  49. });
  50. } else {
  51. // call in the next turn to complete initialization
  52. window.setTimeout((function() {
  53. this.prepGeocoder();
  54. }).createDelegate(this), 0);
  55. }
  56.  
  57. this.store = new Ext.data.JsonStore({
  58. root: "results",
  59. fields: [
  60. {name: "address", type: "string"},
  61. {name: "location"}, // OpenLayers.LonLat
  62. {name: "viewport"} // OpenLayers.Bounds
  63. ],
  64. autoLoad: false
  65. });
  66.  
  67. this.on({
  68. focus: function() {
  69. this.clearValue();
  70. },
  71. scope: this
  72. });
  73.  
  74. return gxp.form.GoogleGeocoderComboBox.superclass.initComponent.apply(this, arguments);
  75.  
  76. },
  77.  
  78. /** private: method[prepGeocoder]
  79. */
  80. prepGeocoder: function() {
  81. var geocoder = new google.maps.Geocoder();
  82.  
  83.  
  84. // create an async proxy for getting geocoder results
  85. var api = {};
  86. api[Ext.data.Api.actions.read] = true;
  87. var proxy = new Ext.data.DataProxy({api: api});
  88. var combo = this;
  89.  
  90. // TODO: unhack this - this is due to the the tool output being generated too early
  91. var getBounds = (function() {
  92. // optional bounds for restricting search
  93. var bounds = this.bounds;
  94. if (bounds) {
  95. if (bounds instanceof OpenLayers.Bounds) {
  96. bounds = bounds.toArray();
  97. }
  98. bounds = new google.maps.LatLngBounds(
  99. new google.maps.LatLng(bounds[1], bounds[0]),
  100. new google.maps.LatLng(bounds[3], bounds[2])
  101. );
  102. }
  103. return bounds;
  104. }).createDelegate(this);
  105.  
  106. proxy.doRequest = function(action, rs, params, reader, callback, scope, options) {
  107. // Assumes all actions read.
  108. geocoder.geocode(
  109. {address: params.query, bounds: getBounds()},
  110. function(results, status) {
  111. var readerResult;
  112. if (status === google.maps.GeocoderStatus.OK ||
  113. status === google.maps.GeocoderStatus.ZERO_RESULTS) {
  114. try {
  115. results = combo.transformResults(results);
  116. readerResult = reader.readRecords({results: results});
  117. } catch (err) {
  118. combo.fireEvent("exception", combo, "response", action, options, status, err);
  119. }
  120. } else {
  121. combo.fireEvent("exception", combo, "remote", action, options, status, null);
  122. }
  123. if (readerResult) {
  124. callback.call(scope, readerResult, options, true);
  125. } else {
  126. callback.call(scope, null, options, false);
  127. }
  128. }
  129. );
  130. };
  131.  
  132. this.store.proxy = proxy;
  133. if (this.initialConfig.disabled != true) {
  134. this.enable();
  135. }
  136. },
  137.  
  138. /** private: method[transformResults]
  139. * Transform an array of results so values are OpenLayers objects.
  140. */
  141. transformResults: function(gResults) {
  142. var num = gResults.length;
  143. var olResults = new Array(num);
  144. var item, latLng, bounds, ne, sw;
  145. for (i=0; i<num; ++i) {
  146. item = gResults[i];
  147. latLng = item.geometry.location;
  148. bounds = item.geometry.viewport;
  149. ne = bounds.getNorthEast();
  150. sw = bounds.getSouthWest();
  151. olResults[i] = {
  152. address: item.formatted_address,
  153. location: new OpenLayers.LonLat(latLng.lng(), latLng.lat()),
  154. viewport: new OpenLayers.Bounds(sw.lng(), sw.lat(), ne.lng(), ne.lat())
  155. };
  156. }
  157. return olResults;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement