http://blog.csdn.net/gisshixisheng/article/details/40540601
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:
[JavaScript]View PlainCopyprint?
- Identify proxy page to use if the ToJson payload to the geometry service is greater than.
- If This is 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.alwaysUseProxy = false;
Next, define the Geometryservice and drawing tools:
[JavaScript]View PlainCopyprint?
- var gsvc = New Geometryservice ("Http://localhost:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
[JavaScript]View PlainCopyprint?
- 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:
[JavaScript]View PlainCopyprint?
- Measuretoolbar.on ("Draw-end", showmeasureresults);
- /**
- * Show 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 = simplifiedgeometries;
- 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:
[JavaScript]View PlainCopyprint?
- 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:
[JavaScript]View PlainCopyprint?
- /**
- * 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 (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");
- }
- });
- Map.on ("Zoom-start", function () {
- Measurediv.css ("display","none");
- });
- Map.on ("zoom-end", function () {
- if (isshow = = true) {
- 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");
- }
- });
- };
The display of the results is shown by a Div, and the process of zooming and moving the map is done.
(GO) Arcgis for JS Geometryservice to measure distances and areas