ArcGIS api for javascript——地理处理任务-服务区域任务
描述
本例展示了如何发送一些输入值到ArcGIS Server上的GIS模型并获得结果反馈给web应用。本例中,模型计算自在地图上单击点起始的驱动时间多边形。 例如,这个分析类型可用于想要知道多少人生活在驾车1,2和3分钟范围内的情况。
这个模型通过ArcGIS Server 地理处理服务提供。查看CalculateDriveTimePolygons服务的服务目录获得模型中可以使用的输入输出。除了默认的信息,模型作者还提供了附加的文档,可以通过单击页面上的"Help URL"访问。
可以创建一个地理处理任务从ArcGIS Server访问自己的模型:
gp = new esri.tasks.Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons");
使用服务目录查出自己的地理处理服务的URL。
当使用一个地理处理任务时,通常配置一组输入参数,然后执行任务。
var params = { "Input_Location":featureSet, "Drive_Times":driveTimes };
gp.execute(params, getDriveTimePolys);
能够选择地包含一个回调函数,当任务执行完成时运行。在本例中回调函数getDriveTimePolys配置结果多边形的符号并将它们加到地图。
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5 <meta http-equiv="X-UA-Compatible" content="IE=7" /> 6 <title>Service Area Task</title> 7 <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.5/js/dojo/dijit/themes/tundra/tundra.css"> 8 <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.5"></script> 9 <script type="text/javascript"> 10 dojo.require("esri.map"); 11 dojo.require("esri.tasks.gp"); 12 13 var map,gp; 14 var driveTimes = "1 2 3"; 15 //初始化地图,gp,和图片参数 16 function init() { 17 var startExtent = new esri.geometry.Extent(-95.41, 38.86, -95.1, 39.06, new esri.SpatialReference({wkid:4326})); 18 map = new esri.Map("mapDiv",{extent :startExtent}); 19 var streeMap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"); 20 map.addLayer(streeMap); 21 22 gp = new esri.tasks.Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons"); 23 24 dojo.connect(map,"onClick",computeServiceArea); 25 26 } 27 28 function computeServiceArea(evt) { 29 //清除所有图层 30 map.graphics.clear(); 31 //设置点 32 var pointSymbol = new esri.symbol.SimpleMarkerSymbol(); 33 //设置 线符号,实线,红色 34 pointSymbol.setOutline = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,255]), 1); 35 pointSymbol.setSize(10);//设置点的大小 36 pointSymbol.setColor(new dojo.Color([0,255,0,0.25]));//设置点的颜色 37 38 //将点击的点的坐标,样式添加到图层 39 var graphic = new esri.Graphic(evt.mapPoint,pointSymbol); 40 map.graphics.add(graphic); 41 42 // 43 var features = []; 44 features.push(graphic); 45 var featureSet = new esri.tasks.FeatureSet(); 46 featureSet.features = features; 47 var params = { "Input_Location":featureSet, "Drive_Times":driveTimes}; 48 gp.execute(params,getDriveTimePolys); 49 } 50 51 function getDriveTimePolys(results,messages) { 52 var features = results[0].value.features; 53 for (var i=0,j=features.length;i<j;i++) { 54 var feature = features[i]; 55 if(i == 0) { 56 var polySymbolRed = new esri.symbol.SimpleFillSymbol(); 57 polySymbolRed.setOutline(new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,0,0.5]), 1)); 58 polySymbolRed.setColor(new dojo.Color([255,0,0,0.7])); 59 feature.setSymbol(polySymbolRed); 60 61 }else if(i == 1) { 62 var polySymbolGreen = new esri.symbol.SimpleFillSymbol(); 63 polySymbolGreen.setOutline(new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,0,0.5]), 1)); 64 polySymbolGreen.setColor(new dojo.Color([0,255,0,0.7])); 65 feature.setSymbol(polySymbolGreen); 66 67 }else if(i == 2) { 68 var polySymbolBlue = new esri.symbol.SimpleFillSymbol(); 69 polySymbolBlue.setOutline(new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,0,0.5]), 1)); 70 polySymbolBlue.setColor(new dojo.Color([0,0,255,0.7])); 71 feature.setSymbol(polySymbolBlue); 72 73 } 74 map.graphics.add(feature); 75 } 76 77 } 78 79 dojo.addOnLoad(init); 80 </script> 81 82 </head> 83 <body class="tundra"> 84 <div id="mapDiv" style="width:800px; height:600px; border:1px solid #000;"></div> 85 Zoom in to a US city and single click the map. A service area GP task will be executed synchronously and on 86 completion the results will be drawn as graphic features to the map. The drive time polygons are 1, 2, 87 and 3 minutes. 88 </body> 89 </html>