让延迟在 esri javascript api 中完成,然后再继续代码 - labelPoints

letting deferred finish in esri javascript api before proceeding with code - labelPoints

我在完成其余代码之前无法完成 labelPoints。我知道这是某种我不理解的 deferred/callback 问题,并且无法找到很多关于如何使用 esri javascript api.

基本上我使用的是质心,我可以从地址搜索中创建一个点击点,但是在奇数形状的多边形上我需要使用 labelPoints 来确保该点实际上在多边形中。

executeQueries() 在 if 中工作正常,但在 else 中它不 运行 正确,因为 executeQueries() 发生在检索 labelPoints 之前。我尝试添加 .on("label-points-complete") 来触发 executeQueries() 但这似乎没有帮助。只是在执行其余部分之前尝试从 labelPoint 获取几何图形。我已经尝试了这 1000 种方法,但都没有成功。感谢您的任何提示。

function symbolizeFoundFeatures(featureSet) {
    //If we get a result set
    if (featureSet.features.length > 0) {
    //Highlight the feature
    var polygonJson = { "rings": featureSet.features[0].geometry.rings, "spatialReference": map.spatialReference };
    var selectedGeometry = featureSet.features[0].geometry;
    searchedGraphic = map.graphics.add(new esri.Graphic(new esri.geometry.Polygon(polygonJson), 
        new esri.symbol.SimpleFillSymbol(
            esri.symbol.SimpleLineSymbol.STYLE_SOLID,
            new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
            new esri.Color([0, 0, 0, 1]), 1),
            new esri.Color([255, 0, 0, 0.2]))
        ));

    selectedCentroid = searchedGraphic.geometry.getExtent().getCenter();
    var markerSymbol = map.graphics.add(new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_SQUARE, 10,
        new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
        new Color([255, 0, 0]), 1),
        new Color([0, 255, 0, 0.25])
    ));
    var searchedGraphicPoint = new Graphic(selectedCentroid, markerSymbol);

//the if here runs everything perfectly
    if (searchedGraphic.geometry.contains(selectedCentroid)) {
        useThisPointToInitiateClick = selectedCentroid;
        executeFromSearchedPoint = "Yes";
        executeQueries();
    }
    else 
      {geometryService.labelPoints([searchedGraphic.geometry], function (labelPoints) {
         console.log("in contains else ");
         var searchedGraphicLabelPoint = new Graphic(labelPoints[0], markerSymbol);
         map.graphics.add(searchedGraphicLabelPoint);
         useThisPointToInitiateClick = labelPoints[0];
         console.log(useThisPointToInitiateClick);
         executeFromSearchedPoint = "Yes";
         console.log("before execute queries, after adding center points");
     });
     }
     }
}

geometryService.on("label-points-complete", function (evt) {
     console.log("hi");
     console.log(executeFromSearchedPoint);
     executeQueries();
});

只需在 labelPoints 回调中调用 executeQueries() 即可?

并删除 geometryService.on("label-points-complete

else 
      {geometryService.labelPoints([searchedGraphic.geometry], function (labelPoints) {
         console.log("in contains else ");
         var searchedGraphicLabelPoint = new Graphic(labelPoints[0], markerSymbol);
         map.graphics.add(searchedGraphicLabelPoint);
         useThisPointToInitiateClick = labelPoints[0];
         console.log(useThisPointToInitiateClick);
         executeFromSearchedPoint = "Yes";
         console.log("before execute queries, after adding center points");
executeQueries();
     });
     }