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(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, myMap.spatialReference);
myMap.infoWindow.setTitle("距离測量");
myMap.infoWindow.setContent(" 測 量 长 度 : <strong>" + parseInt(String(result.lengths[0])) + "米</strong>");
myMap.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, myMap.spatialReference);
myMap.infoWindow.setTitle("面积測量");
myMap.infoWindow.setContent(" 面积 : <strong>" + parseInt(String(result.areas[0])) + "平方米</strong> 周长:" + parseInt(String(result.lengths[0])) + "米");
myMap.infoWindow.show(CurPos);
//alert("面积:" + dojo.number.format(result.areas[0]) + "平方米" + " 长度:" + dojo.number.format(result.lengths[0]) + "米");
}