在完成剩下的代码之前,我无法完成labelPoints。我知道这是某种延迟/回调问题,我不理解,也无法找到很多如何使用esri javascript api执行此操作的示例。
基本上我使用质心,我可以从地址搜索创建一个点击点,但在奇形状的多边形上,我需要使用labelPoints来确保该点实际上在多边形中。
executeQeries()在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();
});
答案 0 :(得分:0)
只需在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();
});
}