Arcgis api for javascript distance and Area Calculation

Source: Internet
Author: User
Tags polyline

In the previous experiment, distance calculation and area calculation had been a problem and it took a long time to find the cause of the problem. Now it is successfully completed.

1. lengthParams. polylines = [geometry];

2. this. outSR = new esri. SpatialReference ({wkid: 102113 });
GeometryService. project ([geometry], this. outSR, function (geometry ){
GeometryService. simplify (geometry, function (simplifiedGeometries ){
AreasAndLengthParams. polygons = simplifiedGeometries;
AreasAndLengthParams. polygons [0]. spatialReference = new esri. SpatialReference (102113 );
GeometryService. areasAndLengths (areasAndLengthParams );
});
});

In addition, the area calculation is different from the distance calculation. projection transformation is required! Because our base map coordinate system is 4326


// Measure function measutreLength () {toolbar. activate (esri. toolbars. draw. POLYLINE);} function measutreArea () {toolbar. activate (esri. toolbars. draw. POLYGON );}

// Metering function doMeasure (geometry) {// display style measuregeometry = geometry; toolbar for more types. deactivate (); switch (geometry. type) {case "polyline": var symbol = new esri. symbol. simpleLineSymbol (esri. symbol. simpleLineSymbol. STYLE_SOLID, new dojo. color ([0, 0, 0]), 2); break; case "polygon": var symbol = new esri. symbol. simpleFillSymbol (esri. symbol. simpleFillSymbol. STYLE_NONE, new esri. symbol. simpleLineSymbol (esri. symbol. simpleLineSymbol. STYLE_DASHDOT, new dojo. color ([255, 0, 0]), 2), new dojo. color ([255,255, 0, 0.25]); break;} // sets the style var graphic = new esri. graphic (geometry, symbol); // clears the last drawing content myMap. graphics. clear (); myMap. graphics. add (graphic); // call projectComplete MeasureGeometry (geometry) after projection conversion is complete;} // call function MeasureGeometry (geometry) after projection conversion is complete) {// if the line type is used, perform lengths distance measurement if (geometry. type = "polyline") {var lengthParams = new esri. tasks. lengthsParameters (); lengthParams. polylines = [geometry]; lengthParams. lengthUnit = esri. tasks. geometryService. UNIT_METER; lengthParams. geodesic = true; lengthParams. polylines [0]. spatialReference = new esri. spatialReference (4326); geometryService. lengths (lengthParams); dojo. connect (geometryService, "onLengthsComplete", outputDistance);} // if it is of the surface type, perform the simplify operation before calculating the area else if (geometry. type = "polygon") {var areasAndLengthParams = new esri. tasks. areasAndLengthsParameters (); areasAndLengthParams. lengthUnit = esri. tasks. geometryService. UNIT_METER; areasAndLengthParams. areaUnit = esri. tasks. geometryService. UNIT_SQUARE_METERS; this. outSR = new esri. spatialReference ({wkid: 102113}); geometryService. project ([geometry], this. outSR, function (geometry) {geometryService. simplify (geometry, function (simplifiedGeometries) {areasAndLengthParams. polygons = simplifiedGeometries; areasAndLengthParams. polygons [0]. spatialReference = new esri. spatialReference (102113); geometryService. areasAndLengths (areasAndLengthParams) ;}); dojo. connect (geometryService, "onAreasAndLengthsComplete", outputAreaAndLength) ;}// display the measurement distance function outputDistance (result) {var CurX = measuregeometry. paths [0] [measuregeometry. paths [0]. length-1] [0]; var CurY = measuregeometry. paths [0] [measuregeometry. paths [0]. length-1] [1]; var CurPos = new esri. geometry. point (CurX, CurY, myMap. spatialReference); myMap. infoWindow. setTitle ("distance measurement"); myMap. infoWindow. setContent ("test duration:"+ ParseInt (String (result. lengths [0]) +" meters"); MyMap. infoWindow. show (CurPos);} // display the measurement area function outputAreaAndLength (result) {var CurX = (measuregeometry. _ extent. xmax + measuregeometry. _ extent. xmin)/2; var CurY = (measuregeometry. _ extent. ymax + measuregeometry. _ extent. ymin)/2 var CurPos = new esri. geometry. point (CurX, CurY, myMap. spatialReference); myMap. infoWindow. setTitle ("Area Measurement"); myMap. infoWindow. setContent ("area:"+ ParseInt (String (result. areas [0]) +" m2Perimeter: "+ parseInt (String (result. lengths [0]) + "meter"); myMap. infoWindow. show (CurPos); // alert ("Area:" + dojo. number. format (result. areas [0]) + "square meter" + "Length:" + dojo. number. format (result. lengths [0]) + "meter ");}


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.