ArcGIS 测量距离和面积以及清除绘画

              //测量距离
              $("#measure1").click(function () {
                    var draw = new Draw(map);
                    //画线
                    draw.activate(Draw.POLYLINE);
                    draw.on("draw-complete", function (evt) {
                        var line = new SimpleLineSymbol();
                        map.graphics.add(new Graphic(evt.geometry, line));
                        draw.deactivate();
                        //将画的线的evt(有geometry属性)传入测距方法中计算距离
                        MeasureAreaorLength(evt, "线");
                    })
                })
                
				//测量面积
               $("#measure2").click(function () {
                    var draw = new Draw(map);
                    //画面
                    draw.activate(Draw.POLYGON);
                    draw.on("draw-complete", function (evt) {
                        var fill = new SimpleFillSymbol();
                        map.graphics.add(new Graphic(evt.geometry, fill));
                        draw.deactivate();
                        //将画的线的evt(有geometry属性)传入测距方法中计算距离
                        MeasureAreaorLength(evt, "面");
                    })
                })
                
				//清除绘画
                $("#BtnDel").click(function(){
                     map.graphics.clear();
                })
                function MeasureAreaorLength(evt, geometrytype) {
                    //判断是测距还是测面
                    if (geometrytype == "线") {
                        //lengths(lengthsParameter, callback?, errback?)中的参数
                        var lengthParams = new LengthsParameters();
                        lengthParams.lengthUnit = GeometryService.UNIT_METER;
                        lengthParams.geodesic = true;
                        lengthParams.polylines = [evt.geometry];
                        //参数传入方法中
                        geoservice.lengths(lengthParams, function (res) {//成功时回调函数
                            console.log("距离是:" + Number(res.lengths[0]).toFixed(3) + "米");
                            $("#divPOLYLINE").val("距离是:" + Number(res.lengths[0]).toFixed(3) + "米");
                        }, function (err) {//失败时回调函数
                            console.log(err)
                        });
                    } else if (geometrytype == "面") {
                        //areasAndLengths(areasAndLengthsParameters, callback?, errback?)中的参数
                        var areasAndLengthParams = new AreasAndLengthsParameters();
                        areasAndLengthParams.areaUnit = GeometryService.UNIT_SQUARE_METERS;
                        //参数传入方法中
                        geoservice.simplify([evt.geometry], function (simplifiedGeometries) {
                            areasAndLengthParams.polygons = simplifiedGeometries;
                            geoservice.areasAndLengths(areasAndLengthParams, function (res) {//成功时回调函数
                                console.log("面积是:" + Number(res.areas[0]).toFixed(3) + "平方米");
                                $("#divPOLYGON").val("面积是:" + Number(res.areas[0]).toFixed(3) + "平方米");
                            });
                        }, function (err) {//失败时回调函数
                            console.log(err)
                        });
                    }
                }

你可能感兴趣的:(前端)