Distance and area measurements are common features of GIS, in this section, the measurement area and distance are described by Geometryservice. Let's look at the results after the implementation:
Distance area
First, make the configuration:
Identify proxy page to use if the ToJson payload to the geometry service is greater than-characters.//if this null Or not available the project and lengths operation won't work. Otherwise it would do a HTTP post to the PROXY.ESRICONFIG.DEFAULTS.IO.PROXYURL = "/proxy"; esriConfig.defaults.io.alwaysUse Proxy = false;
Next, define the Geometryservice and drawing tools:
var gsvc = new Geometryservice ("Http://localhost:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
var measuretoolbar = new Esri.toolbars.Draw (map);
Next, after the drawing finishes adding the drawing to the map and returning the measurement results, increase the Measuretoolbar Draw-end event:
Measuretoolbar.on ("Draw-end", showmeasureresults); /** * Display measurement results * @param evt */var showpt=null; function Showmeasureresults (evt) {measuretoolbar.deactivate (); Map.setmapcursor ("Default"); var geometry = evt.geometry; Switch (geometry.type) {case "polyline": {var length = Geometry.paths[0].length ; SHOWPT = new Point (geometry.paths[0][length-1],map.spatialreference); var lengthparams = new Lengthsparameters (); Lengthparams.lengthunit = Esri.tasks.GeometryService.UNIT_KILOMETER; Lengthparams.polylines = [geometry]; Gsvc.lengths (Lengthparams); Break } Case ' polygon ': {showpt = new Point (GEOMETRY.RINGS[0][0],MAP.SPatialreference); var areasandlengthparams = new Areasandlengthsparameters (); Areasandlengthparams.lengthunit = Esri.tasks.GeometryService.UNIT_KILOMETER; Areasandlengthparams.areaunit = Esri.tasks.GeometryService.UNIT_SQUARE_KILOMETERS; Gsvc.simplify ([Geometry], function (simplifiedgeometries) {areasandlengthparams.polygons = Simp Lifiedgeometries; Gsvc.areasandlengths (Areasandlengthparams); }); Break }} var graphic = new Graphic (geometry, Getgeometrysymbol (Geometry.type)); Map.graphics.add (graphic); }
Depending on the type of geometry, increase the geometryservice Lengths-complete or Areas-and-lengths-complete event:
Gsvc.on ("Lengths-complete", outputlength); function Outputlength (evtobj) { var result = Evtobj.result; Showmeasureinfo (SHOWPT, result.lengths[0].tofixed (3), "km"); Gsvc.on ("Areas-and-lengths-complete", outputareaandlength); function Outputareaandlength (evtobj) { var result = Evtobj.result; Showmeasureinfo (SHOWPT, result.areas[0].tofixed (3), "square kilometer");
Finally, the returned results are displayed on the map:
/** * Show Measurement results * @param SHOWPNT * @param data * @param unit */function Measureinfo (showpnt,data,unit) {var measurediv=$ ("#measure"); var isshow = false; var screenpnt=map.toscreen (SHOWPNT); Measurediv.css ("left", screenpnt.x+ "px"); Measurediv.css ("Top", screenpnt.y+ "px"); Measurediv.css ("position", "absolute"); Measurediv.css ("height", "20px"); MEASUREDIV.CSS ("Display", "block"); Isshow = true; Measurediv.css ("Z-index", "999"); if (unit=== "km") {measurediv.css ("width", "90px"); } else{measurediv.css ("width", "130px"); } $ ("#result"). HTML (data+unit); $ ("#infoclose"). Click (function () {map.graphics.clear (); MEASUREDIV.CSS ("Display", "none"); Isshow = false; }); Map.on ("Pan-start", function () {MEASUREDIV.CSS ("display", "none"); }); Map.on ("Pan-end", function (panend) {if (isshow = = True) {Screenpnt=map.toscreen (s HOWPNT); Measurediv.css ("left", screenpnt.x+ "px"); Measurediv.css ("Top", screenpnt.y+ "px"); Measurediv.css ("position", "absolute"); Measurediv.css ("height", "20px"); MEASUREDIV.CSS ("Display", "block"); } }); Map.on ("Zoom-start", function () {MEASUREDIV.CSS ("display", "none"); }); Map.on ("Zoom-end", function () {if (isshow = = True) {Screenpnt=map.toscreen (SHOWPN T); Measurediv.css ("LeFT ", screenpnt.x+" px "); Measurediv.css ("Top", screenpnt.y+ "px"); Measurediv.css ("position", "absolute"); Measurediv.css ("height", "20px"); MEASUREDIV.CSS ("Display", "block"); } }); };
The display of the results is shown by a Div, and the process of zooming and moving the map is done.
Arcgis for JS Geometryservice to measure distances and areas