Reusable Accessible Mapping Platform

API Docs for: 3.0.0
Show:

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

  1. /*global define, i18n */
  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(GlobalStorage.config.geometryService);
  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. //identify proxy page to use if the toJson payload to the geometry service is greater than 2000 characters.
  116. //If this null or not available the project and lengths operation will not work. Otherwise it will do a http post to the proxy.
  117. esriConfig.defaults.io.proxyUrl = GlobalStorage.config.proxyUrl;
  118. esriConfig.defaults.io.alwaysUseProxy = false;
  119. }
  120. };
  121.  
  122. /**
  123. * Activates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  124. *
  125. * @method activate
  126. * @private
  127. */
  128. function activate() {
  129. measureApp.toolbar.activate(Draw.LINE);
  130.  
  131. displayOutput(i18n.t(that.ns + ":na"));
  132. }
  133.  
  134. /**
  135. * Deactivates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  136. *
  137. * @method deactivate
  138. * @private
  139. */
  140. function deactivate() {
  141. measureApp.toolbar.deactivate();
  142. clearMap();
  143. }
  144.  
  145. /**
  146. * Clears the map. This method is passed to the `initToggle` method as the `defaultAction`
  147. * to be triggered by the BaseTool logic when the `float-default-button` is clicked.
  148. *
  149. * @method clearMap
  150. * @private
  151. */
  152. function clearMap() {
  153. measureApp.map.graphics.clear();
  154.  
  155. displayOutput(i18n.t(that.ns + ":na"));
  156. }
  157.  
  158. /**
  159. * Displays the tool's output by calling BaseTool's `displayOutput` function.
  160. *
  161. * @method displayOutput
  162. * @private
  163. */
  164. function displayOutput(length, lengthUnits) {
  165. that.displayTemplateOutput(
  166. {
  167. lengthLabel: i18n.t(that.ns + ":length"),
  168. lengthOutput: length,
  169. lengthUnits: lengthUnits
  170. }
  171. );
  172. }
  173.  
  174. return dojoLang.mixin({}, BaseTool, {
  175. /**
  176. * Initialize the population tool
  177. *
  178. * @method init
  179. * @constructor
  180. *
  181. */
  182. init: function (selector, d) {
  183. that = this;
  184.  
  185. this.initToggle(selector, d,
  186. {
  187. activate: activate,
  188. deactivate: deactivate,
  189. defaultAction: clearMap
  190. }
  191. );
  192.  
  193. ui.init();
  194.  
  195. return this;
  196. },
  197.  
  198. name: "distanceTool"
  199. });
  200. });