Arcgis for JS Geometryservice to measure distances and areas

Source: Internet
Author: User
Tags http post

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?
    1. Identify proxy page to use if the ToJson payload to the geometry service is greater than.
    2. 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.
    3. ESRICONFIG.DEFAULTS.IO.PROXYURL = "/proxy";
    4. EsriConfig.defaults.io.alwaysUseProxy = false;

Next, define the Geometryservice and drawing tools:

[JavaScript]View PlainCopyprint?
    1. var gsvc = New Geometryservice ("Http://localhost:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
[JavaScript]View PlainCopyprint?
    1. 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?
  1. Measuretoolbar.on ("Draw-end", showmeasureresults);
  2. /**
  3. * Show Measurement results
  4. * @param evt
  5. */
  6. var showpt=null;
  7. function Showmeasureresults (evt) {
  8. Measuretoolbar.deactivate ();
  9. Map.setmapcursor ("Default");
  10. var geometry = evt.geometry;
  11. switch (geometry.type) {
  12. case "polyline": {
  13. var length = geometry.paths[0].length;
  14. SHOWPT = new Point (geometry.paths[0][length-1],map.spatialreference);
  15. var lengthparams = new Lengthsparameters ();
  16. Lengthparams.lengthunit = Esri.tasks.GeometryService.UNIT_KILOMETER;
  17. Lengthparams.polylines = [geometry];
  18. Gsvc.lengths (Lengthparams);
  19. Break ;
  20. }
  21. case "polygon": {
  22. SHOWPT = new Point (geometry.rings[0][0],map.spatialreference);
  23. var areasandlengthparams = new Areasandlengthsparameters ();
  24. Areasandlengthparams.lengthunit = Esri.tasks.GeometryService.UNIT_KILOMETER;
  25. Areasandlengthparams.areaunit = Esri.tasks.GeometryService.UNIT_SQUARE_KILOMETERS;
  26. Gsvc.simplify ([geometry], function (simplifiedgeometries) {
  27. Areasandlengthparams.polygons = simplifiedgeometries;
  28. Gsvc.areasandlengths (Areasandlengthparams);
  29. });
  30. Break ;
  31. }
  32. }
  33. var graphic = new graphic (geometry, Getgeometrysymbol (Geometry.type));
  34. Map.graphics.add (graphic);
  35. }

Depending on the type of geometry, increase the geometryservice Lengths-complete or Areas-and-lengths-complete event:

[JavaScript]View PlainCopyprint?
  1. Gsvc.on ("Lengths-complete", outputlength);
  2. function Outputlength (evtobj) {
  3. var result = Evtobj.result;
  4. Showmeasureinfo (SHOWPT, result.lengths[0].tofixed (3), "km");
  5. };
  6. Gsvc.on ("Areas-and-lengths-complete", outputareaandlength);
  7. function Outputareaandlength (evtobj) {
  8. var result = Evtobj.result;
  9. Showmeasureinfo (SHOWPT, result.areas[0].tofixed (3), "square kilometer");
  10. };

Finally, the returned results are displayed on the map:

[JavaScript]View PlainCopyprint?
  1. /**
  2. * Show Measurement results
  3. * @param showpnt
  4. * @param data
  5. * @param unit
  6. */
  7. function Measureinfo (showpnt,data,unit) {
  8. var measurediv=$ ("#measure");
  9. var isshow = false;
  10. var screenpnt=map.toscreen (SHOWPNT);
  11. Measurediv.css ("left", screenpnt.x+"px");
  12. Measurediv.css ("Top", screenpnt.y+"px");
  13. Measurediv.css ("position","absolute");
  14. Measurediv.css ("height","20px");
  15. Measurediv.css ("display","block");
  16. Isshow = true;
  17. Measurediv.css ("Z-index","999");
  18. if (unit==="km") {
  19. Measurediv.css ("width","90px");
  20. }
  21. else{
  22. Measurediv.css ("width","130px");
  23. }
  24. $ ("#result"). HTML (data+unit);
  25. $ ("#infoclose"). Click (function () {
  26. Map.graphics.clear ();
  27. Measurediv.css ("display","none");
  28. Isshow = false;
  29. });
  30. Map.on ("Pan-start", function () {
  31. Measurediv.css ("display","none");
  32. });
  33. Map.on ("pan-end", function (panend) {
  34. if (isshow = = true) {
  35. Screenpnt=map.toscreen (SHOWPNT);
  36. Measurediv.css ("left", screenpnt.x+"px");
  37. Measurediv.css ("Top", screenpnt.y+"px");
  38. Measurediv.css ("position","absolute");
  39. Measurediv.css ("height","20px");
  40. Measurediv.css ("display","block");
  41. }
  42. });
  43. Map.on ("Zoom-start", function () {
  44. Measurediv.css ("display","none");
  45. });
  46. Map.on ("zoom-end", function () {
  47. if (isshow = = true) {
  48. Screenpnt=map.toscreen (SHOWPNT);
  49. Measurediv.css ("left", screenpnt.x+"px");
  50. Measurediv.css ("Top", screenpnt.y+"px");
  51. Measurediv.css ("position","absolute");
  52. Measurediv.css ("height","20px");
  53. Measurediv.css ("display","block");
  54. }
  55. });
  56. };


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.