当前位置: 首页 > news >正文

7timer.info 免费天气预报对接记录

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.client.RestTemplate;import java.time.*;
import java.time.format.DateTimeFormatter;public class SevenTimerWeather {public static void main(String[] args) throws Exception {String url = "http://www.7timer.info/bin/astro.php"+ "?lon=114.54503&lat=38.138258&ac=0&lang=en&unit=metric&output=json&tzshift=0";RestTemplate restTemplate = new RestTemplate();String json = restTemplate.getForObject(url, String.class);ObjectMapper mapper = new ObjectMapper();JsonNode root = mapper.readTree(json);// 起报时间(UTC)String initStr = root.get("init").asText();LocalDateTime initUtc = LocalDateTime.parse(initStr, DateTimeFormatter.ofPattern("yyyyMMddHH"));ZonedDateTime initBj = initUtc.atZone(ZoneOffset.UTC).withZoneSameInstant(ZoneId.of("Asia/Shanghai"));// 当前北京时间ZonedDateTime nowBj = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));// 选择最接近当前时间的预报JsonNode dataseries = root.get("dataseries");JsonNode bestNode = null;long bestDiff = Long.MAX_VALUE;for (JsonNode node : dataseries) {int tp = node.get("timepoint").asInt();ZonedDateTime forecastTime = initBj.plusHours(tp);long diff = Math.abs(Duration.between(forecastTime, nowBj).toMinutes());if (diff < bestDiff) {bestDiff = diff;bestNode = node;}}if (bestNode != null) {int temp = bestNode.get("temp2m").asInt();                        // 室外温度int rh = bestNode.get("rh2m").asInt();                             // 湿度%String windDir = bestNode.get("wind10m").get("direction").asText();int windSpeed = bestNode.get("wind10m").get("speed").asInt();int transparency = bestNode.get("transparency").asInt();double feelsLike = calcFeelsLike(temp, rh, windSpeed);String windDirCn = windDirToChinese(windDir);String visibility = transparencyToKm(transparency);ZonedDateTime forecastTime = initBj.plusHours(bestNode.get("timepoint").asInt());System.out.println("预报时间:" + forecastTime);System.out.println("室外温度:" + temp + " ℃");System.out.println("体感温度:" + String.format("%.1f ℃", feelsLike));System.out.println("风向:" + windDirCn);System.out.println("风速:" + windSpeed + " m/s");System.out.println("能见度:" + visibility);String weatherText = getWeatherText(bestNode.get("cloudcover").asInt(),bestNode.get("prec_type").asText());System.out.println("天气:" + weatherText);}}// 根据 cloudcover 和 prec_type 推断天气文本private static String getWeatherText(int cloudcover, String precType) {if ("snow".equalsIgnoreCase(precType)) {if (cloudcover >= 7) return "大雪";else if (cloudcover >= 4) return "中雪";else return "小雪";} else if ("rain".equalsIgnoreCase(precType)) {if (cloudcover >= 7) return "大雨";else if (cloudcover >= 4) return "中雨";else return "小雨";} else { // 无降水if (cloudcover <= 2) return "晴";else if (cloudcover <= 5) return "少云/多云";else if (cloudcover <= 7) return "阴";else return "阴天";}}// 风向中文转换private static String windDirToChinese(String dir) {switch (dir) {case "N": return "北风";case "NE": return "东北风";case "E": return "东风";case "SE": return "东南风";case "S": return "南风";case "SW": return "西南风";case "W": return "西风";case "NW": return "西北风";default: return dir;}}// 透明度 -> 能见度公里数private static String transparencyToKm(int t) {switch (t) {case 1: return "≥20 km";case 2: return "16–20 km";case 3: return "12–16 km";case 4: return "8–12 km";case 5: return "4–8 km";case 6: return "2–4 km";case 7: return "1–2 km";case 8: return "0.5–1 km";case 9: return "<0.5 km";default: return "未知";}}// 简易体感温度计算private static double calcFeelsLike(double tempC, int humidity, double windMs) {if (tempC >= 27) {// 热指数公式 (近似)return -8.784695 + 1.61139411 * tempC + 2.338549 * humidity- 0.14611605 * tempC * humidity- 0.012308094 * tempC * tempC- 0.016424828 * humidity * humidity+ 0.002211732 * tempC * tempC * humidity+ 0.00072546 * tempC * humidity * humidity- 0.000003582 * tempC * tempC * humidity * humidity;} else if (tempC <= 10) {// 风寒指数公式double windKmh = windMs * 3.6;return 13.12 + 0.6215 * tempC - 11.37 * Math.pow(windKmh, 0.16)+ 0.3965 * tempC * Math.pow(windKmh, 0.16);} else {return tempC;}}
}
  • lat纬度 (Latitude)

    • 表示南北位置

    • 北纬为正,南纬为负

  • lon经度 (Longitude)

    • 表示东西位置

    • 东经为正,西经为负

坐标系使用WGS84格式,可以在谷歌地图拾取

http://www.jsqmd.com/news/4117/

相关文章:

  • 牛客刷题-Day5
  • 详细介绍:四大金刚之计算机网络
  • 用标准版平板干翻上代Pro,小米又想学苹果了?
  • VonaJS多租户同时支持共享模式和独立模式
  • 记录一下第一次为Dify贡献插件的经历
  • 物联网字节校验常用方法
  • 实用指南:RabbitMQ 核心组件详解与持久化日志队列实现方案
  • 实用指南:【C语言】统计二进制中1的个数:三种方法的比较与分析
  • Visual Prompt Builder-AI 提示词可视化工具 - 详解
  • STM32H743-ARM例程2-UART命令控制LED - 实践
  • 完整教程:Zookeeper与Kafka:分布式系统中的协调与消息队列
  • vite-vue3 项目优化首屏加载速度
  • 深入解析:小九源码-springboot050-基于spring boot的苏蔚家校互联管理系统
  • 12_TCP和UDP实现服务端和客户端的通信
  • 各种软件的官方文档和安装包下载地址记录
  • 基于导频的OFDM系统的信道估计(使用LS估计算法)
  • Day22super详解
  • 外发图纸如何控制的最佳实践与注意事项
  • Gitee:中国开发者生态的数字底座正在重构技术格局
  • 快递100
  • 文件同步软件是什么?主要有哪几种类型?
  • “铸网2025”山东省工业和互联网CTF竞赛-web
  • python+springboot+uniapp微信小代码“美好食荐”框架 美食推荐 菜谱展示 用户互动 评论收藏框架
  • 领嵌iLeadE-588网关AI边缘计算盒子一键部署二次开发
  • 2025年值得选的文件摆渡系统品牌解析
  • 分布式专题——14 RabbitMQ之集群实战 - 指南
  • QT打包工具
  • QT与Spring Boot通信:实现HTTP请求的完整指南 - 教程
  • 全球知名的Java Web开发平台Vaadin上线慧都网!
  • C#实现与欧姆龙PLC通信