日期:2014-05-16  浏览次数:20424 次

arcgis api for javascript 距离与面积量算

在之前的实验中,距离量算跟面积量算一直出问题,费了很长的时间,各种调式找不到原因。现在成功完成,与君共勉

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);
            });
        });

还有面积量算中跟距离量算有所不同,需要进行投影变换!因为一般咱们的底图坐标系为4326


//量测
function measutreLength() {
    toolbar.activate(esri.toolbars.Draw.POLYLINE);
}
function measutreArea() {
    toolbar.activate(esri.toolbars.Draw.POLYGON);
}

//量算
function doMeasure(geometry) {
    //更加类型设置显示样式
    measuregeometry = geometry; 
    toolbar.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;
    }
    //设置样式
    var graphic = new esri.Graphic(geometry,symbol);
    //清除上一次的画图内容
    myMap.graphics.clear();
    myMap.graphics.add(graphic);
    //进行投影转换,完成后调用projectComplete
    MeasureGeometry(geometry);
}

//投影转换完成后调用方法
function MeasureGeometry(geometry) {
    //如果为线类型就进行lengths距离测算
    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);
    }
    //如果为面类型需要先进行simplify操作在进行面积测算
    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(g