在JavaScript中,可以使用以下方式以度为单位显示风向:
windDirection
。let windDirection = 180; // 假设风向为180度(南风)
if (windDirection >= 337.5 || windDirection < 22.5) {
console.log("北风"); // 0°-22.5° 或 337.5°-360°
} else if (windDirection >= 22.5 && windDirection < 67.5) {
console.log("东北风"); // 22.5°-67.5°
} else if (windDirection >= 67.5 && windDirection < 112.5) {
console.log("东风"); // 67.5°-112.5°
} else if (windDirection >= 112.5 && windDirection < 157.5) {
console.log("东南风"); // 112.5°-157.5°
} else if (windDirection >= 157.5 && windDirection < 202.5) {
console.log("南风"); // 157.5°-202.5°
} else if (windDirection >= 202.5 && windDirection < 247.5) {
console.log("西南风"); // 202.5°-247.5°
} else if (windDirection >= 247.5 && windDirection < 292.5) {
console.log("西风"); // 247.5°-292.5°
} else if (windDirection >= 292.5 && windDirection < 337.5) {
console.log("西北风"); // 292.5°-337.5°
}
上述代码将根据给定的windDirection
变量输出对应的风向文本。
function getWindDirection(degrees) {
if (degrees >= 337.5 || degrees < 22.5) {
return "北风";
} else if (degrees >= 22.5 && degrees < 67.5) {
return "东北风";
} else if (degrees >= 67.5 && degrees < 112.5) {
return "东风";
} else if (degrees >= 112.5 && degrees < 157.5) {
return "东南风";
} else if (degrees >= 157.5 && degrees < 202.5) {
return "南风";
} else if (degrees >= 202.5 && degrees < 247.5) {
return "西南风";
} else if (degrees >= 247.5 && degrees < 292.5) {
return "西风";
} else if (degrees >= 292.5 && degrees < 337.5) {
return "西北风";
}
}
let windDirection = 180; // 假设风向为180度(南风)
console.log(getWindDirection(windDirection)); // 输出:"南风"
这样,通过调用getWindDirection
函数,并传入风向的角度,即可获得对应的风向文本。
腾讯云的相关产品和产品介绍链接地址请参考腾讯云官方网站。
领取专属 10元无门槛券
手把手带您无忧上云