简体   繁体   English

为什么此回调不返回JavaScript中的值?

[英]Why doesn't this callback return a value in JavaScript?

gsvc.project(params, function (projectedPoints) {
    pt = projectedPoints[0];
});

geometry.addPath(pt);

Can anyone tell me why projectedPoints only returns once an error is thrown? 谁能告诉我为什么projectedPoints仅在抛出错误后才返回? The code doesn't enter the callback and tries to assign pt while it is undefined to an array. 该代码不会进入回调,而是在未定义数组时尝试分配pt。 This causes the code to error, which is when the callback finally initiates. 这会导致代码出错,也就是回调最终启动时。

I've been trying to reference the code from here , but can't seem to figure out how to get the callback to work like in the xample. 我一直在尝试从此处引用代码,但似乎无法弄清楚如何使回调像在xample中那样工作。

` var map, gsvc, pt; `var map,gsvc,pt;

  require([
    "esri/map", "esri/graphic", "esri/symbols/SimpleMarkerSymbol",
    "esri/tasks/GeometryService", "esri/tasks/ProjectParameters",
    "esri/SpatialReference", "esri/InfoTemplate", "dojo/dom", "dojo/on",
    "dojo/domReady!"
  ], function(
    Map, Graphic, SimpleMarkerSymbol,
    GeometryService, ProjectParameters,
    SpatialReference, InfoTemplate, dom, on
  ) {
    map = new Map("map", {
      basemap: "streets",
      center: [-98.445, 46.147],
      zoom: 3
    });

    gsvc = new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
    map.on("click", projectToWebMercator);

    function projectToWebMercator(evt) {
      map.graphics.clear();

      var point = evt.mapPoint;
      var symbol = new SimpleMarkerSymbol().setStyle("diamond");
      var graphic = new Graphic(point, symbol);
      var outSR = new SpatialReference(102100);

      map.graphics.add(graphic);

      gsvc.project([ point ], outSR, function(projectedPoints) {
        pt = projectedPoints[0];
        graphic.setInfoTemplate(new InfoTemplate("Coordinates",
          "<span>X:</span>" + pt.x.toFixed() + "<br>" + 
          "<span>Y:</span>" + pt.y.toFixed() + "<br>" + 
          "<input type='button' value='Convert back to LatLong' id='convert'>" +
          "<div id='latlong'></div>"));
        map.infoWindow.setTitle(graphic.getTitle());
        map.infoWindow.setContent(graphic.getContent());
        map.infoWindow.show(evt.screenPoint, map.getInfoWindowAnchor(evt.screenPoint));
        on.once(dom.byId("convert"), "click", projectToLatLong);
      });
    }

    function projectToLatLong() {
      var outSR = new SpatialReference(4326);
      var params = new ProjectParameters();
      params.geometries = [pt.normalize()];
      params.outSR = outSR;

      gsvc.project(params, function(projectedPoints) {
        pt = projectedPoints[0];
        dom.byId("latlong").innerHTML = "<span>Latitude: </span> " + 
          pt.y.toFixed(3) + "<br><span>Longitude:</span>" + pt.x.toFixed(3);
      });
    }
  });
</script>`

You have a variable scope issue. 您有一个可变范围问题。 pt needs to be defined outside your callback. pt需要在回调之外定义。

var pt;

gsvc.project(params, function(projectedPoints) {
    pt = projectedPoints[0];
});

geometry.addPath(pt);

EDIT: 编辑:

If the .project() method is asynchronous, then Arun's answer is the correct approach. 如果.project()方法是异步的,那么Arun的答案就是正确的方法。

It might be because project is a async method if so 可能是因为项目是异步方法

//looks like project is a async method
gsvc.project(params, function (projectedPoints) {
    var pt = projectedPoints[0];
    //do all operations which depends on pt in the callback
    geometry.addPath(pt);
});

//so this part will get executed before the callback is executed so pt won't be defined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM