Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

js 异步 回调

function coordinate(result,Point,SpatialReference,GeometryService,ProjectParameters){
    var data=[];
    for(var i=0;i<result.length;i++){
         var geometryService = new GeometryService("http://localhost:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
         var incoord = 4549;  //当前坐标系
         var outcoord = 4490;  //目标坐标系
         var x=result[i]["x"];
         var y=result[i]["y"];
         var inSR = new SpatialReference({wkid: incoord });
         var outSR = new SpatialReference({wkid: outcoord});
         var inputpoint = new Point(x,y, inSR);
         var PrjParams = new ProjectParameters();
         PrjParams.geometries = [inputpoint];
         PrjParams.outSR = outSR;
         geometryService.project(PrjParams,function(outputpoint){
           data.push(outputpoint);
           });
        } 
    return data;
}
$(document).ready(function(){
    require([
             "esri/geometry/Point","esri/SpatialReference","esri/tasks/GeometryService","esri/tasks/ProjectParameters",
             "dojo/domReady!"
         ], function(Point,SpatialReference,GeometryService,ProjectParameters) {
    var type=$(":radio[name='optionsRadios']:checked").val();
    var stime=$("#class_time option:selected").val();
    var data=[];
    $.ajax({
        type:"post",
        async:true,
        url:"../zj/Aggregation.action",
        data:{"type":parseInt(type),"stime":stime},
        dataType: "json", //返回数据形式为json
        success:function(result){
            var d=result.slice(0,3);
            var data=coordinate(d,Point,SpatialReference,GeometryService,ProjectParameters);    
            console.log(data);
        }
    });
});
});

需要将数据添加到data里面然后返回,但geometryService.project这个方法里面有个回调函数.第一张图是输出结果,但那个空的数组点开是由内容的。

图片描述
图片描述


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
function coordinate(result, Point, SpatialReference, GeometryService, ProjectParameters) {
    return Promise.all(result.map(function(item) {
      return Promise(function(resolve){
        var geometryService = new GeometryService("http://localhost:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
        var incoord = 4549; //当前坐标系
        var outcoord = 4490; //目标坐标系
        var x = item["x"];
        var y = item["y"];
        var inSR = new SpatialReference({
            wkid: incoord
        });
        var outSR = new SpatialReference({
            wkid: outcoord
        });
        var inputpoint = new Point(x, y, inSR);
        var PrjParams = new ProjectParameters();
        PrjParams.geometries = [inputpoint];
        PrjParams.outSR = outSR;
        geometryService.project(PrjParams, function(outputpoint) {
            resolve(outputpoint);
        });
      })
    }))
}
$(document).ready(function() {
    require([
        "esri/geometry/Point", "esri/SpatialReference", "esri/tasks/GeometryService", "esri/tasks/ProjectParameters",
        "dojo/domReady!"
    ], function(Point, SpatialReference, GeometryService, ProjectParameters) {
        var type = $(":radio[name='optionsRadios']:checked").val();
        var stime = $("#class_time option:selected").val();
        var data = [];
        $.ajax({
            type: "post",
            async: true,
            url: "../zj/Aggregation.action",
            data: {
                "type": parseInt(type),
                "stime": stime
            },
            dataType: "json", //返回数据形式为json
            success: function(result) {
                var d = result.slice(0, 3);
                var pro = coordinate(d, Point, SpatialReference, GeometryService, ProjectParameters);

                //使用了Promise,如果兼容低版本浏览器,请引入相应的Promise库
                //这样pro是一个Promise对象,可以使用
                /*pro.then(function(data){
                    //这里可以操作所有的data
                  }).catch(function(err){
                    throw new Erroe(err)
                  })
                */

            }
        });
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...