[英]Sending parameters from different functions to a function
function layerQuery(objectId) {
const featureLayer = view.map.layers.getItemAt(0);
const queryParams = featureLayer.createQuery();
queryParams.where = "objectid=" + objectId;
queryParams.outFields = ["x", "y", "z"];
featureLayer.queryFeatures(queryParams).then(function (results) {
const coords = results.features[0].attributes;
direction_lookup(Promise, Promise,coords.x,coords.y)
});
const objectIdNext = objectId + 1
const queryParamsTb = featureLayer.createQuery();
queryParamsTb.where = "objectid=" + objectIdNext;
queryParamsTb.outFields = ["x", "y", "z"];
featureLayer.queryFeatures(queryParamsTb).then(function (results) {
var coordstb = results.features[0].attributes;
direction_lookup(coordstb.x, coordstb.y,Promise,Promise)
});
//console.log(coordstb.x)
}
我想把我們上面兩個函數的結果得到的四個參數發送給下面的function。
function direction_lookup(destination_x, origin_x, destination_y, origin_y) {
var compass_brackets, compass_lookup, degrees_final, degrees_temp, deltaX, deltaY;
deltaX = destination_x - origin_x;
deltaY = destination_y - origin_y;
degrees_temp = Math.atan2(deltaX, deltaY) / Math.PI * 180;
if (degrees_temp < 0) {
degrees_final = 360 + degrees_temp;
} else {
degrees_final = degrees_temp;
}
compass_brackets = ["N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"];
compass_lookup = Math.round(degrees_final / 45);
return [compass_brackets[compass_lookup], degrees_final];
}
console.log(direction_lookup(destination_x, origin_x, destination_y, origin_y));
'direction lookup' function 有四個參數。 我想從不同的函數中兩個兩個地發送這四個參數。 我可以這樣做嗎
您嘗試執行的操作是不可能的,因為您的direction_lookup
function 需要這四個參數作為數字坐標,它不希望看到任何Promise
對象或解析它們。 執行此操作的標准方法是等到知道所有值后再調用 function,如下所示:
function layerQuery(objectId) {
const featureLayer = view.map.layers.getItemAt(0);
const queryParams = featureLayer.createQuery();
queryParams.where = "objectid=" + objectId;
queryParams.outFields = ["x", "y", "z"];
featureLayer.queryFeatures(queryParams).then((results) => {
const feature1Coords = results.features[0].attributes;
const objectIdNext = objectId + 1
const queryParamsTb = featureLayer.createQuery();
queryParamsTb.where = "objectid=" + objectIdNext;
queryParamsTb.outFields = ["x", "y", "z"];
featureLayer.queryFeatures(queryParamsTb).then((secondResults) => {
const feature2Coords = secondResults.features[0].attributes;
direction_lookup(feature2Coords.x, features2Coords.y,feature1Coords.x,feature1Coords.y);
});
});
}
現在,您執行第一個查詢並在執行第二個查詢時保留 scope 中的結果,最后在准備好 go 的兩組坐標時調用direction_lookup
。
箭頭函數的行為類似於常規函數,但保留父函數 scope,這可以避免很多混亂,因此對於此類任務,它是一種稍微方便一些的表示法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.