将参数从不同的函数发送到一个函数
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 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'函数有四个参数。我想从不同的函数中两个两个地发送这四个参数。我可以这样做吗
您尝试执行的操作是不可能的,因为您的 direction_lookup
函数需要这四个参数作为数字 co-ordinates,它不希望看到任何 Promise
对象或解决它们。执行此操作的标准方法是等到知道所有值后再调用函数,如下所示:
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);
});
});
}
现在执行第一个查询并在执行第二个查询时将结果保留在范围内,最后在两组 co-ordinates 准备就绪时调用 direction_lookup
。
Arrow functions 表现得像常规函数,但保留了父作用域,这可以避免很多混乱,所以对于这种任务,它是一种稍微方便一些的表示法。
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 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'函数有四个参数。我想从不同的函数中两个两个地发送这四个参数。我可以这样做吗
您尝试执行的操作是不可能的,因为您的 direction_lookup
函数需要这四个参数作为数字 co-ordinates,它不希望看到任何 Promise
对象或解决它们。执行此操作的标准方法是等到知道所有值后再调用函数,如下所示:
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);
});
});
}
现在执行第一个查询并在执行第二个查询时将结果保留在范围内,最后在两组 co-ordinates 准备就绪时调用 direction_lookup
。
Arrow functions 表现得像常规函数,但保留了父作用域,这可以避免很多混乱,所以对于这种任务,它是一种稍微方便一些的表示法。