ArcGIS api for javascript—测量,测距问题

首先量测

  var map, toolbar, geometryService ;
      function init() {
      
       map=new esri.Map("map",{
	          extent:new esri.geometry.Extent({xmin:110,ymin:31 ,xmax:117 ,ymax :37,spatialReference:{wkid:4326} }) 
	    });//选取的空间坐标系范围 
	
        dojo.connect(map, "onLoad", initToolbar);
       
        map.addLayer(new esri.layers.ArcGISTiledMapServiceLayer("url");
       
      }

 function initToolbar(map) {
       
        toolbar = new esri.toolbars.Draw(map); 
        
        //添加toolbar画图完成后事件监听调用    doMeasure
        dojo.connect(toolbar, "onDrawEnd", doMeasure);
        //实例化GeometryService,地址为ags发布的GeometryServer

         var geomrtryserver ="url2";
        geometryService = new esri.tasks.GeometryService(geomrtryserver);
 
      }

  

//量测
	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);  
    //清除上一次的画图内容  
    map.graphics.clear();  
    map.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(geometryService, "onAreasAndLengthsComplete", outputAreaAndLength);  
    }  
}  
  
//显示测量距离  
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, map.spatialReference);  
    map.infoWindow.setTitle("距离测量");  
    map.infoWindow.setContent(" 测 量 长 度 : <strong>" + parseInt(String(result.lengths[0])) + "米</strong>");  
    map.infoWindow.show(CurPos);  
}  
  
//显示测量面积  
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, map.spatialReference);  
    map.infoWindow.setTitle("面积测量");  
    map.infoWindow.setContent(" 面积 : <strong>" + parseInt(String(result.areas[0])) + "平方米</strong> 周长:" + parseInt(String(result.lengths[0])) + "米");  
    map.infoWindow.show(CurPos);  
    //alert("面积:" + dojo.number.format(result.areas[0]) + "平方米" + " 长度:" + dojo.number.format(result.lengths[0]) + "米");  
}  
	

  

 

posted @ 2017-03-31 15:17  xiaotian_小天  Views(3196)  Comments(1Edit  收藏  举报