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

news/2025/9/26 11:29:56/文章来源:https://www.cnblogs.com/rolayblog/p/19113166
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.mzph.cn/news/918183.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

招聘网站开发程序员做国外订单的网站

环境 Python3&#xff0c; gensim&#xff0c;jieba&#xff0c;numpy &#xff0c;pandas 原理&#xff1a;文章转成向量&#xff0c;然后在计算两个向量的余弦值。 Gensim gensim是一个python的自然语言处理库&#xff0c;能够将文档根据TF-IDF, LDA, LSI 等模型转化成向量模…

牛客刷题-Day5

动态规划1:线性dp、背包问题,区间 https://ac.nowcoder.com/acm/contest/24213?from=acdiscussn牛客刷题-Day5 今日刷题:\(1021-1025\) 1021 失衡天平 题目描述 终于 \(Alice\) 走出了大魔王的陷阱,可是现在傻傻的…

详细介绍:四大金刚之计算机网络

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

用标准版平板干翻上代Pro,小米又想学苹果了?

9月20日,小米合伙人卢伟冰在直播中揭晓了小米平板8系列的完整配置。 从整体配置到系列产品来看,小米平板8的发布再次印证了小米的产品策略:用标准版打上代Pro。 这熟悉的配方,熟悉的味道,不禁让人想起了大洋彼岸的…

VonaJS多租户同时支持共享模式和独立模式

VonaJS 通过多实例的概念来支持多租户 SAAS 系统的开发。只需启动一个后端服务,即可支持多个实例同时运行。同时支持共享模式和独立模式。多实例/多租户 VonaJS 通过多实例的概念来支持多租户 SAAS 系统的开发。只需启…

记录一下第一次为Dify贡献插件的经历

最近Dify上线了一个新功能——知识管道(Knowledge Pipeline)。知识管道可以像乐高一样编排你的信息,以数据源(Data Source)作为起始节点,以知识库节点作为结束节点。其一般步骤为:从数据源导入文档 -> 使用抽…

免费自媒体网站有创意的设计公司名称

1. 使用 systemd 服务设置开机自启动 假设已经有一个可执行的python程序&#xff0c;然后用一个sh脚本去启动python程序&#xff0c;正常情况使用挂起的方式nohup启动&#xff0c;日志输出到指定文件&#xff1a; sudo touch run.sh sudo chmod 777 run.shsh文件内容如下&…

物联网字节校验常用方法

① 校验和(Checksum)原理:把所有字节加起来(可能取低 8 位 / 16 位),作为校验值。 优点:实现极其简单,计算快,资源消耗小。 缺点:检测能力有限(部分错误无法发现,例如两个字节互换位置)。 应用场景:早期…

实用指南:RabbitMQ 核心组件详解与持久化日志队列实现方案

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

实用指南:【C语言】统计二进制中1的个数:三种方法的比较与分析

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

Visual Prompt Builder-AI 提示词可视化工具 - 详解

Visual Prompt Builder-AI 提示词可视化工具 - 详解2025-09-26 11:18 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; disp…

STM32H743-ARM例程2-UART命令控制LED - 实践

STM32H743-ARM例程2-UART命令控制LED - 实践pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", &quo…

大连做网站哪家便宜深圳市龙华区房价

作者&#xff1a;激越王预估稿费&#xff1a;400RMB投稿方式&#xff1a;发送邮件至linwei#360.cn&#xff0c;或登陆网页版在线投稿你是否听说过xml注入攻击呢&#xff0c;或者对它只知其一不知其二呢&#xff1f;现在让我们从xml相关基础知识开始&#xff0c;一步步了解xml攻…

建设科技处网站wordpress wap

目录 说明批量zip2pdf批量zip2pdf下载SS号重命名源代码SS号重命名源代码下载附录&#xff0c;水文年鉴 说明 1、zip2pdf是一个开源软件&#xff0c;支持自动化解压压缩包成PDG&#xff0c;PDG合成PDF&#xff0c;笔者在其基础上做了部分修改&#xff0c;支持批量转换。 2、秒…

提供做网站公司wordpress开启多站点

我们组件中 会有很多通用的信息和方法 那么 首先 我们看通用事件 通用事件中 最常用的就是我们的点击事件 比如说 我们之前常写的 组件.onClick(()>{//事件逻辑 })但是 我们之前 都没有用它接参数 我们可以这样 Button("跳转").onClick((ewat: ClickEvent)>…

网站建设html代码邢台信息港二手房出售

1.简介 双指针技巧是一种常见的算法解题方法&#xff0c;通过使用两个指针在数据结构上同时移动&#xff0c;可以解决多种问题。这种技巧通常适用于数组、字符串和链表等数据结构&#xff0c;下面我将详细介绍双指针技巧的特点和应用场景&#xff1a; 特点&#xff1a; 快慢…

完整教程:Zookeeper与Kafka:分布式系统中的协调与消息队列

完整教程:Zookeeper与Kafka:分布式系统中的协调与消息队列pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Conso…

vite-vue3 项目优化首屏加载速度

A嵌入B,B加载速度太慢了,需要4s+,需优化: B系统技术栈: vue3 + vite 优化结果如下,上下对比还有有差距的:一、先处理: 首屏加载的文件——先优化文件大 step1: 安装可视化插件 rollup-plugin-visualizer; vi…

深入解析:小九源码-springboot050-基于spring boot的苏蔚家校互联管理系统

深入解析:小九源码-springboot050-基于spring boot的苏蔚家校互联管理系统pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-famil…

建设自己公司的网站公司网站本地如何弄

目录 前言 一、Mybatis-Plus 开启日志的方式 二、测试 三、日志分析 章末 前言 小伙伴们大家好&#xff0c;相信大家平时在处理问题时都有各自的方式&#xff0c;最常用以及最好用的感觉还是断点调试&#xff0c;但是涉及到操作数据库的执行时&#xff0c;默认的话在控制台…