I'm encountering an issue with obtaining my labelPoints before completing the rest of my code. It seems to be related to a deferred/callback problem that I can't quite grasp, and I couldn't find many examples using the esri javascript api.
In essence, I utilize centroids for creating a click point from an address search, but for irregular shaped polygons, I need to use labelPoints to ensure the point falls within the polygon.
The executeQueries() function works fine in the if statement, but in the else block it doesn't execute correctly because the labelPoints retrieval happens after executeQueries(). I attempted to use .on("label-points-complete") to trigger the executeQueries(), but it doesn't seem to work. I just want to get the geometry from the labelPoint before proceeding further. I've tried numerous methods without success. Any suggestions are appreciated. Thank you.
function symbolizeFoundFeatures(featureSet) {
//If we have 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();
});