1. 程式人生 > >arcgis for js實現GP服務建立、釋出、呼叫(呼叫GP釋出服務)

arcgis for js實現GP服務建立、釋出、呼叫(呼叫GP釋出服務)

1.本例中使用的地圖服務和GP服務都可以從arcgis server manage中拿到;

2.資料型別,這是GP服務的資料型別。每一個GP服務資料型別和JS資料型別進行對應,比如緩衝區模型製作中使用的FeatureClass,,釋出服務說明(對於說明可以參考這裡http://www.cnblogs.com/HPhone/archive/2012/11/18/2775860.html)中可以看到Data Type為GPFeatureRecordSetLayer 則arcgis for js api中使用FeatureSet物件進行構造參;


3.輸入要素和輸出要素名稱可以在此處檢視



呼叫程式碼如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>利用GP呼叫自定義buffer工具實現緩衝區分析功能</title>
    <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>
    <style>
        html, body, #map {
            width:   100%;
            height:  100%;
            margin:  0;
            padding: 0;
        }
    </style>
    <script>
        require([
            "esri/Color",
            "esri/layers/ArcGISTiledMapServiceLayer",
            "esri/map",
            "esri/symbols/SimpleFillSymbol",
            "esri/symbols/SimpleLineSymbol",
            "esri/toolbars/draw",
            "esri/symbols/SimpleMarkerSymbol",
            "esri/graphic",
            "esri/geometry/Extent",
            "esri/tasks/Geoprocessor",
            "esri/SpatialReference",
            "esri/tasks/FeatureSet",
            "esri/tasks/JobInfo",
            "esri/tasks/LinearUnit"
        ], function(
             Color,
             ArcGISTiledMapServiceLayer,
             Map,SimpleFillSymbol,
             SimpleLineSymbol,
             Draw,
             SimpleMarkerSymbol,
             Graphic,
             Extent,
             Geoprocessor,
             SpatialReference,
             FeatureSet,
             JobInfo,
             LinearUnit
        ) {
            var map;
            map = new Map("map", {
                extent: new Extent({
                    "xmin":126.08797131337525,"ymin":41.88483304829672,"xmax":130.05572254059723,"ymax":47.20292839632739,
                    "spatialReference":{"wkid":4326}})
            });
            var Layer = new ArcGISTiledMapServiceLayer("http://localhost:6080/arcgis/rest/services/itms/MapServer", {
                "id": "layer",
                "opacity": 0.75,
                "showAttribution":false
            });
            map.addLayer(Layer);
            var  gpBuffer=new Geoprocessor("http://localhost:6080/arcgis/rest/services/Model/GPServer/bufferModel");
            gpBuffer.outSpatialReference=new SpatialReference({wkid:4326});
            var bufferParams={};
            map.on("click",function (e) {
                debugger;
                var featureSet=new FeatureSet();
                var graphics=new Graphic(e.mapPoint,new SimpleMarkerSymbol());
                featureSet.spatialReference=new SpatialReference({wkid:4326});
                map.graphics.add(graphics);
                featureSet.features=[graphics];
                featureSet.geometryType="point";
                var Dis = new LinearUnit();
                Dis.distance = 0.1;
                Dis.units = esri.Units.KILOMETERS;
                bufferParams={
                    Feature_Class:featureSet,    //輸入要素名稱要和發服務的引數名稱一致
                    Distance:Dis                  //距離名稱要和發服務的距離引數一致
                };
                gpBuffer.submitJob(bufferParams,successResult);
            })
            function successResult(result){
                var jobId = result.jobId;
                var status = result.jobStatus;
                if(status ===JobInfo.STATUS_SUCCEEDED) {
                    gpBuffer.getResultData(jobId, "Output_Feature_Class", addResults);    //第二個引數要和發服務的結果集名稱一致
                }
            }
            function addResults(results){
                var features = results.value.features;
                if(features.length>0) {
                    for (var i = 0, length = features.length; i != length; ++i) {
                        var feature = features[i];
                        var polySymbolRed = new SimpleFillSymbol();
                        polySymbolRed.setOutline(new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 0, 0, 0.5]), 1));
                        polySymbolRed.setColor(new Color([255, 0, 0, 0.5]));
                        feature.setSymbol(polySymbolRed);
                        map.graphics.add(feature);
                    }
                    alert("提示計算成功!");
                }
                else{
                    alert("提示計算失敗!");
                }
            }
        });

    </script>
</head>
<body>
<div id="map"></div>
</body>
</html>