1.arcgis for js中实现计算面积和周长需要两个服务地址,一个为地图切片资源地址,一个为计算面积和长度的工具服务地址,都可以在本地发布的arcgis server manager中拿到;
显示js代码
var map, tb;
var geometryService;
require([
"esri/map", "esri/toolbars/draw",
"esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleFillSymbol",
"esri/symbols/PictureFillSymbol", "esri/symbols/CartographicLineSymbol",
"esri/graphic",
"esri/Color", "dojo/dom", "dojo/on",
"esri/layers/ArcGISTiledMapServiceLayer",
"esri/tasks/GeometryService","esri/SpatialReference","esri/tasks/ProjectParameters","esri/tasks/AreasAndLengthsParameters",
"dojo/domReady!"
], function(
Map, Draw,
SimpleMarkerSymbol, SimpleFillSymbol,
PictureFillSymbol, CartographicLineSymbol,
Graphic,
Color, dom, on,
ArcGISTiledMapServiceLayer,GeometryService,SpatialReference,ProjectParameters,AreasAndLengthsParameters
) {
map = new Map("mapDiv", {
"xmin":126.08797131337525,"ymin":41.88483304829672,"xmax":130.05572254059723,"ymax":47.20292839632739,
"spatialReference":{"wkid":4326}
});
map.on("load", initToolbar);
var oilAndGasLayer = new ArcGISTiledMapServiceLayer("http://localhost:6080/arcgis/rest/services/itms/MapServer");
map.addLayer(oilAndGasLayer);
geometryService=GeometryService("http://localhost:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
geometryService.on("areas-and-lengths-complete", function(evt) {
var result = evt.result;
dom.byId("area").innerHTML = result.areas[0].toFixed(3) + " acres";
dom.byId("length").innerHTML = result.lengths[0].toFixed(3) + " feet";
});
function initToolbar() {
tb = new Draw(map);
tb.on("draw-complete", addGraphic);
tb.activate(Draw.FREEHAND_POLYGON);
}
function addGraphic(evt) {
var geometry = evt.geometry;
map.graphics.clear();
map.graphics.add(new Graphic(geometry, new SimpleFillSymbol()));
var areasAndLengthParams = new AreasAndLengthsParameters();
areasAndLengthParams.lengthUnit = GeometryService.UNIT_FOOT;
areasAndLengthParams.areaUnit = GeometryService.UNIT_ACRES;
areasAndLengthParams.calculationType = "geodesic";
geometryService.simplify([geometry], function (simplifiedGeometries) {
areasAndLengthParams.polygons = simplifiedGeometries;
geometryService.areasAndLengths(areasAndLengthParams);
});
}
});
创建DIV为
<body>
<div id="mapDiv" style="height: 95%"></div>
Length: <span id="length"></span><br>
Area: <span id="area"></span>
</body>
引用资源类为(实现资源类需要本地搭建arcgis api服务)
<link rel="stylesheet" type="text/css" href="http://localhost:8087/arcgis_js_api/library/3.22/3.22/esri/css/esri.css" />
<script src="http://localhost:8087/arcgis_js_api/library/3.22/3.22/init.js"></script>
<script src="../js/jquery-1.3.1.js"></script>