Reusable Accessible Mapping Platform

API Docs for: 5.0.0
Show:

File: src\js\RAMP\Tools\distanceTool.js

  1. /*global define, i18n, RAMP */
  2.  
  3. /**
  4. * MeasureTool submodule.
  5. *
  6. * Computes the area and perimeter length of a selected area. When the user draws a polygon, the area
  7. * and length will be displayed in the bottom right corner.
  8. *
  9. * @module RAMP
  10. * @submodule MeasureTool
  11. * @main MeasureTool
  12. */
  13.  
  14. /**
  15. * MeasureTool class.
  16. *
  17. * @class MeasureTool
  18. * @static
  19. * @uses dojo/dom
  20. * @uses dojo/_base/lang
  21. * @uses esri/config
  22. * @uses esri/graphic
  23. * @uses esri/tasks/GeometryService
  24. * @uses esri/tasks/LengthsParameters
  25. * @uses esri/toolbars/draw
  26. * @uses esri/symbols/SimpleFillSymbol
  27. * @uses Map
  28. * @uses GlobalStorage
  29. */
  30.  
  31. define([
  32. // Dojo
  33. "dojo/dom", "dojo/string", "dojo/_base/lang",
  34. // Esri
  35. "esri/config", "esri/graphic", "esri/tasks/GeometryService",
  36. "esri/tasks/LengthsParameters", "esri/toolbars/draw", "esri/symbols/SimpleFillSymbol",
  37. // Ramp
  38. "ramp/map", "ramp/globalStorage", "tools/baseTool"
  39. ],
  40. function (
  41. // Dojo
  42. dom, string, dojoLang,
  43. // Esri
  44. esriConfig, Graphic, GeometryService, LengthsParameters, Draw, SimpleFillSymbol,
  45. // Ramp
  46. RampMap, GlobalStorage, BaseTool) {
  47. "use strict";
  48.  
  49. var ui,
  50. geometryService,
  51. measureApp,
  52. that;
  53.  
  54. /**
  55. * Compute the area and length of a specified polygon.
  56. *
  57. * @method computeAreaAndLength
  58. * @private
  59. * @param {Object} evtObj an object representing the event.
  60. *
  61. */
  62. function computeAreaAndLength(evtObj) {
  63. that.working(true);
  64.  
  65. geometryService = new GeometryService(RAMP.config.geometryServiceUrl);
  66. geometryService.on("lengths-complete", outputAreaAndLength);
  67.  
  68. var geometry = evtObj.geometry;
  69. measureApp.map.graphics.clear();
  70.  
  71. measureApp.map.graphics.add(new Graphic(geometry, new SimpleFillSymbol()));
  72.  
  73. //setup the parameters for the areas and lengths operation
  74. var lengthsParams = new LengthsParameters();
  75. lengthsParams.lengthUnit = GeometryService.UNIT_KILOMETER;
  76. lengthsParams.calculationType = "geodesic";
  77.  
  78. geometryService.simplify([geometry], function (simplifiedGeometries) {
  79. lengthsParams.polylines = simplifiedGeometries;
  80. geometryService.lengths(lengthsParams);
  81. });
  82. }
  83.  
  84. /**
  85. * Display the calculated area and length on the map.
  86. *
  87. * @method outputAreaAndLength
  88. * @private
  89. * @param {Object} evtObj an object representing the event.
  90. *
  91. */
  92. function outputAreaAndLength(evtObj) {
  93. var result = evtObj.result,
  94. // Convert feet to km.
  95. length = result.lengths[0].toFixed(3);// (result.lengths[0] / 3280.8).toFixed(3);
  96.  
  97. that.working(false);
  98.  
  99. length = string.substitute("${number:dojo.number.format}", { number: length });
  100. displayOutput(length, "km");
  101. }
  102.  
  103. ui = {
  104. init: function () {
  105. var map = RampMap.getMap(),
  106. toolbar = new Draw(map);
  107.  
  108. toolbar.on("draw-end", computeAreaAndLength);
  109.  
  110. measureApp = {
  111. map: map,
  112. toolbar: toolbar
  113. };
  114. }
  115. };
  116.  
  117. /**
  118. * Activates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  119. *
  120. * @method activate
  121. * @private
  122. */
  123. function activate() {
  124. measureApp.toolbar.activate(Draw.LINE);
  125.  
  126. displayOutput(i18n.t(that.ns + ":na"));
  127. }
  128.  
  129. /**
  130. * Deactivates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  131. *
  132. * @method deactivate
  133. * @private
  134. */
  135. function deactivate() {
  136. measureApp.toolbar.deactivate();
  137. clearMap();
  138. }
  139.  
  140. /**
  141. * Clears the map. This method is passed to the `initToggle` method as the `defaultAction`
  142. * to be triggered by the BaseTool logic when the `float-default-button` is clicked.
  143. *
  144. * @method clearMap
  145. * @private
  146. */
  147. function clearMap() {
  148. measureApp.map.graphics.clear();
  149.  
  150. displayOutput(i18n.t(that.ns + ":na"));
  151. }
  152.  
  153. /**
  154. * Displays the tool's output by calling BaseTool's `displayOutput` function.
  155. *
  156. * @method displayOutput
  157. * @private
  158. */
  159. function displayOutput(length, lengthUnits) {
  160. that.displayTemplateOutput(
  161. {
  162. lengthLabel: i18n.t(that.ns + ":length"),
  163. lengthOutput: length,
  164. lengthUnits: lengthUnits
  165. }
  166. );
  167. }
  168.  
  169. return dojoLang.mixin({}, BaseTool, {
  170. /**
  171. * Initialize the population tool
  172. *
  173. * @method init
  174. * @constructor
  175. *
  176. */
  177. init: function (selector, d) {
  178. that = this;
  179.  
  180. this.initToggle(selector, d,
  181. {
  182. activate: activate,
  183. deactivate: deactivate,
  184. defaultAction: clearMap
  185. }
  186. );
  187.  
  188. ui.init();
  189.  
  190. return this;
  191. },
  192.  
  193. name: "distanceTool"
  194. });
  195. });