vue使用的百度地图的天气查询功能

news/2024/11/3 1:35:35/

首先需要在 Vue.js 项目中安装百度地图 JavaScript API SDK,并获取相应的密钥(ak)。然后,可以按照以下步骤使用百度地图的天气查询功能并使用 Axios 进行请求:

官网文档:https://lbsyun.baidu.com/faq/api?title=webapi/weather/base

  1. index.html 中添加百度地图 JavaScript API SDK 的引用:
<script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=YOUR_AK"></script>

请将 YOUR_AK 替换为你自己在百度地图开放平台申请的密钥。

  1. 在需要使用天气查询功能的组件中,用以下代码创建一个名为 map 的百度地图实例:
created() {const map = new BMap.Map("map-container");this.map = map;
}

其中 "map-container" 是容器元素的 ID,可以根据需要替换成其他值。同时,需要在 data 属性中添加 map 字段,以便在后续使用中保持对该实例的引用。

  1. 创建一个 getWeather 方法,用于向百度地图天气 API 发送请求并处理返回结果:
methods: {async getWeather() {try {const res = await axios.get("https://api.map.baidu.com/weather/v1/?district_id=110000&data_type=all&ak=YOUR_AK");// 处理返回结果} catch (error) {console.error(error);}}
}

请将 YOUR_AK 替换为你自己在百度地图开放平台申请的密钥。此处使用了 Axios 库发送 GET 请求,并将返回结果存储在 res 变量中。

  1. 在组件模板中,添加一个按钮来触发 getWeather 方法:
<button @click="getWeather">查询天气</button>
{"status":0,"result":{"location":{"country":"中国","province":"北京市","city":"北京市","name":"东城","id":"110101"},"now":{"temp":4,"feels_like":1,"rh":73,"wind_class":"2级","wind_dir":"东风","text":"多云","prec_1h":0,"clouds":999999,"vis":3471,"aqi":140,"pm25":107,"pm10":0,"no2":23,"so2":22,"o3":70,"co":1.7,"uptime":"20200220143500"},"indexes":[{"name":"晨练指数","brief":"较适宜","detail":"天气阴沉,请避免在林中晨练。"},{"name":"洗车指数","brief":"适宜","detail":"天气较好,适合擦洗汽车。"},{"name":"感冒指数","brief":"易发","detail":"天凉,昼夜温差大,易感冒"},{"name":"紫外线指数","brief":"最弱","detail":"辐射弱,涂擦SPF8-12防晒护肤品。"},{"name":"穿衣指数","brief":"较冷","detail":"建议着厚外套加毛衣等服装。"},{"name":"运动指数","brief":"较适宜","detail":"气温较低,在户外运动请注意增减衣物。"}],"alerts":[{"type": "道路冰雪","level": "蓝色预警","title": "市气象局发布道路冰雪蓝色预警[IV级/一般]", "desc": "市气象局发布道路冰雪蓝色预警信号:受降雪天气影响,预计未来 24 小时我市将出现对交通有影响的道路结冰或积雪, 请有关部门及广大群众做好防范工作。"}],"forecasts":[{"date":"2020-02-20","week":"星期四","high":7,"low":-2,"wc_day":"<3级","wc_night":"<3级","wd_day":"东南风","wd_night":"北风","text_day":"多云","text_night":"阴","aqi":93},{"date":"2020-02-21","week":"星期五","high":11,"low":1,"wc_day":"4~5级","wc_night":"<3级","wd_day":"西北风","wd_night":"西北风","text_day":"多云","text_night":"晴","aqi":44},{"date":"2020-02-22","week":"星期六","high":10,"low":-2,"wc_day":"<3级","wc_night":"<3级","wd_day":"西风","wd_night":"南风","text_day":"晴","text_night":"晴","aqi":39},{"date":"2020-02-23","week":"星期日","high":11,"low":0,"wc_day":"<3级","wc_night":"<3级","wd_day":"北风","wd_night":"北风","text_day":"晴","text_night":"晴","aqi":65},{"date":"2020-02-24","week":"星期一","high":9,"low":-1,"wc_day":"<3级","wc_night":"<3级","wd_day":"东风","wd_night":"东北风","text_day":"多云","text_night":"多云","aqi":38},{"date":"2020-02-25","week":"星期二","high":9,"low":-3,"wc_day":"<3级","wc_night":"<3级","wd_day":"东南风","wd_night":"西南风","text_day":"晴","text_night":"晴","aqi":27},{"date":"2020-02-26","week":"星期三","high":9,"low":-3,"wc_day":"<3级","wc_night":"<3级","wd_day":"西南风","wd_night":"西南风","text_day":"晴","text_night":"晴","aqi":26}],"forecast_hours":[{"text":"晴","temp_fc":14,"wind_class":"3~4级","wind_dir":"西南风","rh":15,"prec_1h":0,"clouds":10,"data_time":"2020-04-01 16:00:00"},{"text":"晴","temp_fc":14,"wind_class":"3~4级","wind_dir":"西南风","rh":13,"prec_1h":0,"clouds":10,"data_time":"2020-04-01 17:00:00"},{"text":"晴","temp_fc":13,"wind_class":"<3级","wind_dir":"西南风","rh":14,"prec_1h":0,"clouds":10,"data_time":"2020-04-01 18:00:00"},{"text":"晴","temp_fc":11,"wind_class":"<3级","wind_dir":"西南风","rh":15,"prec_1h":0,"clouds":10,"data_time":"2020-04-01 19:00:00"},{"text":"晴","temp_fc":10,"wind_class":"<3级","wind_dir":"西南风","rh":16,"prec_1h":0,"clouds":10,"data_time":"2020-04-01 20:00:00"},{"text":"晴","temp_fc":9,"wind_class":"<3级","wind_dir":"西风","rh":18,"prec_1h":0,"clouds":6,"data_time":"2020-04-01 21:00:00"},{"text":"晴","temp_fc":9,"wind_class":"<3级","wind_dir":"西风","rh":20,"prec_1h":0,"clouds":3,"data_time":"2020-04-01 22:00:00"},{"text":"晴","temp_fc":8,"wind_class":"<3级","wind_dir":"西风","rh":21,"prec_1h":0,"clouds":0,"data_time":"2020-04-01 23:00:00"},{"text":"晴","temp_fc":7,"wind_class":"<3级","wind_dir":"西北风","rh":26,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 00:00:00"},{"text":"晴","temp_fc":6,"wind_class":"<3级","wind_dir":"西北风","rh":31,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 01:00:00"},{"text":"晴","temp_fc":6,"wind_class":"<3级","wind_dir":"西北风","rh":36,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 02:00:00"},{"text":"晴","temp_fc":5,"wind_class":"<3级","wind_dir":"西北风","rh":39,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 03:00:00"},{"text":"晴","temp_fc":4,"wind_class":"<3级","wind_dir":"西北风","rh":42,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 04:00:00"},{"text":"晴","temp_fc":4,"wind_class":"<3级","wind_dir":"西北风","rh":45,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 05:00:00"},{"text":"晴","temp_fc":5,"wind_class":"<3级","wind_dir":"西北风","rh":40,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 06:00:00"},{"text":"晴","temp_fc":7,"wind_class":"<3级","wind_dir":"西北风","rh":34,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 07:00:00"},{"text":"晴","temp_fc":8,"wind_class":"<3级","wind_dir":"西北风","rh":29,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 08:00:00"},{"text":"晴","temp_fc":11,"wind_class":"<3级","wind_dir":"西北风","rh":29,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 09:00:00"},{"text":"晴","temp_fc":13,"wind_class":"<3级","wind_dir":"西北风","rh":29,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 10:00:00"},{"text":"晴","temp_fc":16,"wind_class":"<3级","wind_dir":"西北风","rh":29,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 11:00:00"},{"text":"晴","temp_fc":17,"wind_class":"3~4级","wind_dir":"西北风","rh":24,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 12:00:00"},{"text":"晴","temp_fc":18,"wind_class":"3~4级","wind_dir":"西北风","rh":19,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 13:00:00"},{"text":"晴","temp_fc":19,"wind_class":"3~4级","wind_dir":"西北风","rh":14,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 14:00:00"},{"text":"晴","temp_fc":19,"wind_class":"<3级","wind_dir":"西风","rh":17,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 15:00:00"}]},"message":"success"
}

这样,在用户点击按钮时就会向百度地图天气 API 发送请求并处理结果。

注意:以上代码仅为示例,实际项目中可能需要根据实际情况进行修改和调整。

我们更多的是让后端在服务器进行请求,然后把数据返回给前端,这样可以减少跨域和代理的麻烦!

官网文档:https://lbsyun.baidu.com/faq/api?title=webapi/weather/base


http://www.ppmy.cn/news/118580.html

相关文章

【03.05】大数据教程--JavaScript

当谈到大数据处理时&#xff0c;JavaScript 是一种广泛使用的语言&#xff0c;它具有强大的前端和后端能力。在本教程中&#xff0c;我们将介绍一些有关使用 JavaScript 处理大数据的基本知识&#xff0c;并通过使用 Python 语言编写示例代码来说明。 步骤 1&#xff1a;准备工…

OA考勤系统源代码交付包含哪些功能?

OA智能考核系统一般包括功能有:打卡、多班次设置、排班、人脸识别打卡、考勤自动计算处理和丰富的考勤报表等。 企典软件OA考勤系统源代码交付功能有: 1.灵活考勤班次&#xff1a;企业可以通过电脑和移动端双端操作&#xff0c;设定相应的上下班时间、迟到、午休、加班、就餐…

【刷题】KY11 二叉树遍历

KY11 二叉树遍历 一、题目描述二、示例三、实现 KY11 二叉树遍历 一、题目描述 编一个程序&#xff0c;读入用户输入的一串先序遍历字符串&#xff0c;根据此字符串建立一个二叉树&#xff08;以指针方式存储&#xff09;。 例如如下的先序遍历字符串&#xff1a; ABC##DE#G#…

把VS Code打造成后端开发的宇宙IDE,也挺爽

本文首发自「慕课网」&#xff0c;想了解更多IT干货内容&#xff0c;程序员圈内热闻&#xff0c;欢迎关注&#xff01; 作者&#xff1a;维生素P|慕课网优质作者 工欲善其事必先利其器&#xff0c;提高程序员的开发效率必须要有一个好的开发工具。而一旦提到开发工具&#xff…

了解并使用 SOCKS5 代理和代理 IP 进行网络连接

本文将介绍 SOCKS5 代理和代理 IP 的概念&#xff0c;探讨它们在网络连接中的作用和应用。我们将了解如何设置和配置 SOCKS5 代理&#xff0c;并讨论代理 IP 的用途和优势。此外&#xff0c;还将探讨 SOCKS5 代理和代理 IP 在隐私保护和访问控制方面的应用 在当今互联网环境中&…

Ocam录屏软件安装包以及使用说明

链接&#xff1a;https://pan.baidu.com/s/1muliES1Kuh2rj2cppa4jxQ 提取码&#xff1a;iplw 使用方法在我的资源中&#xff0c;欢迎收看使用~

班迪录屏注册机(Bandicam)

链接&#xff1a;https://pan.baidu.com/s/1O0s-4_Xz4DdCuTejVB4d7g 提取码&#xff1a;qb50

Clion软件汉化

对于有很多英语不太好的家人们&#xff0c;语言的汉化是必不可少的&#xff0c;因为如果英语不好&#xff0c;会使开发效率大大降低&#xff0c;而且稍有不慎就会操作失误&#xff0c;一朝回到解放前&#xff0c;这里就详细介绍一下CLion是如何进行汉化的吧&#xff01; 首先&…