高德地图API之获取天气

标签: 无 分类: 未分类 创建时间:2023-04-26 06:19:19 更新时间:2025-01-17 10:39:24

1.前言

获取高德的天气接口,可以使用 webapi 构造http请求,也可以通过 jsapi 进行获取。

1.安装依赖
2.配置密钥
3.配置 Web端的 key

参考文章:
1.vue 高德天气接口,高德自定义天气图片 这里有下载图片的地方,给出的代码是使用的vue2进行编写的。
2.vue 高德天气接口,高德自定义天气图片 估计是一个人,但是这里的下载资源还要钱,又好像不是一个人
3.高德地图天气接口返回天气类型图标url 这里使用的是 amap-jsapi-loader。
4.天气查询 这是使用接口的是方式进行天气调用的。

2.代码

代码其实很简单的,就是这个图片获取还是有些麻烦的,因为还需要在网盘 中获取。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<template>
<img :src="weaterImgUrl" class="weater-img"/>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue"
import AMapLoader from "@amap/amap-jsapi-loader"

const baseUrl = import.meta.env.VITE_BASE_URL
const weaterImgUrl = ref(baseUrl + "/img/weather/weather01.png")

// 天气列表
const weatherList = [
{ id: 1, name: "晴", url: baseUrl + "/img/weather/weather01.png" },
{ id: 2, name: "少云", url: baseUrl + "/img/weather/weather02.png" },
{ id: 3, name: "晴间多云", url: baseUrl + "/img/weather/weather03.png" },
{ id: 4, name: "多云", url: baseUrl + "/img/weather/weather04.png" },
{ id: 5, name: "阴", url: baseUrl + "/img/weather/weather05.png" },
{ id: 6, name: "有风", url: baseUrl + "/img/weather/weather06.png" },
{ id: 7, name: "平静", url: baseUrl + "/img/weather/weather07.png" },
{ id: 8, name: "微风", url: baseUrl + "/img/weather/weather08.png" },
{ id: 9, name: "和风", url: baseUrl + "/img/weather/weather09.png" },
{ id: 10, name: "清风", url: baseUrl + "/img/weather/weather10.png" },
{ id: 11, name: "强风/劲风", url: baseUrl + "/img/weather/weather11.png" },
{ id: 12, name: "疾风", url: baseUrl + "/img/weather/weather12.png" },
{ id: 13, name: "大风", url: baseUrl + "/img/weather/weather13.png" },
{ id: 14, name: "烈风", url: baseUrl + "/img/weather/weather14.png" },
{ id: 15, name: "风暴", url: baseUrl + "/img/weather/weather15.png" },
{ id: 16, name: "狂爆风", url: baseUrl + "/img/weather/weather16.png" },
{ id: 17, name: "飓风", url: baseUrl + "/img/weather/weather17.png" },
{ id: 18, name: "热带风暴", url: baseUrl + "/img/weather/weather18.png" },
{ id: 19, name: "霾", url: baseUrl + "/img/weather/weather19.png" },
{ id: 20, name: "中度霾", url: baseUrl + "/img/weather/weather20.png" },
{ id: 21, name: "重度霾", url: baseUrl + "/img/weather/weather21.png" },
{ id: 22, name: "严重霾", url: baseUrl + "/img/weather/weather22.png" },
{ id: 23, name: "阵雨", url: baseUrl + "/img/weather/weather23.png" },
{ id: 24, name: "雷阵雨", url: baseUrl + "/img/weather/weather24.png" },
{ id: 25, name: "雷阵雨并伴有冰雹", url: baseUrl + "/img/weather/weather25.png" },
{ id: 26, name: "小雨", url: baseUrl + "/img/weather/weather26.png" },
{ id: 27, name: "中雨", url: baseUrl + "/img/weather/weather27.png" },
{ id: 28, name: "大雨", url: baseUrl + "/img/weather/weather28.png" },
{ id: 29, name: "暴雨", url: baseUrl + "/img/weather/weather29.png" },
{ id: 30, name: "大暴雨", url: baseUrl + "/img/weather/weather30.png" },
{ id: 31, name: "特大暴雨", url: baseUrl + "/img/weather/weather31.png" },
{ id: 32, name: "强阵雨", url: baseUrl + "/img/weather/weather32.png" },
{ id: 33, name: "强雷阵雨", url: baseUrl + "/img/weather/weather33.png" },
{ id: 34, name: "极端降雨", url: baseUrl + "/img/weather/weather34.png" },
{ id: 35, name: "毛毛雨/细雨", url: baseUrl + "/img/weather/weather35.png" },
{ id: 36, name: "雨", url: baseUrl + "/img/weather/weather36.png" },
{ id: 37, name: "小雨-中雨", url: baseUrl + "/img/weather/weather37.png" },
{ id: 38, name: "中雨-大雨", url: baseUrl + "/img/weather/weather38.png" },
{ id: 39, name: "大雨-暴雨", url: baseUrl + "/img/weather/weather39.png" },
{ id: 40, name: "暴雨-大暴雨", url: baseUrl + "/img/weather/weather40.png" },
{ id: 41, name: "大暴雨-特大暴雨", url: baseUrl + "/img/weather/weather41.png" },
{ id: 42, name: "雨雪天气", url: baseUrl + "/img/weather/weather42.png" },
{ id: 43, name: "雨夹雪", url: baseUrl + "/img/weather/weather43.png" },
{ id: 44, name: "阵雨夹雪", url: baseUrl + "/img/weather/weather44.png" },
{ id: 45, name: "冻雨", url: baseUrl + "/img/weather/weather45.png" },
{ id: 46, name: "雪", url: baseUrl + "/img/weather/weather46.png" },
{ id: 47, name: "阵雪", url: baseUrl + "/img/weather/weather47.png" },
{ id: 48, name: "小雪", url: baseUrl + "/img/weather/weather48.png" },
{ id: 49, name: "中雪", url: baseUrl + "/img/weather/weather49.png" },
{ id: 50, name: "大雪", url: baseUrl + "/img/weather/weather50.png" },
{ id: 51, name: "暴雪", url: baseUrl + "/img/weather/weather51.png" },
{ id: 52, name: "小雪-中雪", url: baseUrl + "/img/weather/weather52.png" },
{ id: 53, name: "中雪-大雪", url: baseUrl + "/img/weather/weather53.png" },
{ id: 54, name: "大雪-暴雪", url: baseUrl + "/img/weather/weather54.png" },
{ id: 55, name: "浮尘", url: baseUrl + "/img/weather/weather55.png" },
{ id: 56, name: "扬沙", url: baseUrl + "/img/weather/weather56.png" },
{ id: 57, name: "沙尘暴", url: baseUrl + "/img/weather/weather57.png" },
{ id: 58, name: "强沙尘暴", url: baseUrl + "/img/weather/weather58.png" },
{ id: 59, name: "龙卷风", url: baseUrl + "/img/weather/weather59.png" },
{ id: 60, name: "雾", url: baseUrl + "/img/weather/weather60.png" },
{ id: 61, name: "浓雾", url: baseUrl + "/img/weather/weather61.png" },
{ id: 62, name: "强浓雾", url: baseUrl + "/img/weather/weather62.png" },
{ id: 63, name: "轻雾", url: baseUrl + "/img/weather/weather63.png" },
{ id: 64, name: "大雾", url: baseUrl + "/img/weather/weather64.png" },
{ id: 65, name: "特强浓雾", url: baseUrl + "/img/weather/weather65.png" },
{ id: 66, name: "热", url: baseUrl + "/img/weather/weather66.png" },
{ id: 67, name: "冷", url: baseUrl + "/img/weather/weather67.png" },
{ id: 68, name: "未知", url: baseUrl + "/img/weather/weather68.png" }
]

/**
* 执行加载
*/
window._AMapSecurityConfig = { // 加载密钥
serviceHost: "xxx"
}
AMapLoader.load({
key: "xxx", // 申请好的Web端开发者Key,首次调用 load 时必填
version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
plugins: ["AMap.CitySearch,AMap.Geolocation,AMap.Weather"] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
}).then((AMap) => {
const citySearch = new AMap.CitySearch()
citySearch.getLocalCity((status:any, result:any) => {
if (status === "complete" && result.info === "OK") {
// 查询成功,result即为当前所在城市信息
console.log(result)
} else { // 使用定位接口进行定位
const geolocation = new AMap.Geolocation({
enableHighAccuracy: true, // 是否使用高精度定位,默认:true
timeout: 10000, // 设置定位超时时间,默认:无穷大
offset: [10, 20], // 定位按钮的停靠位置的偏移量
zoomToAccuracy: true, // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
position: "RB" // 定位按钮的排放位置, RB表示右下
})

geolocation.getCityInfo(function(status:any, result:any) {
if (status === "complete") { // result 是具体的定位信息
const citycode = result.citycode
}
})
}
})

// 获取天气信息
const weather = new AMap.Weather()
const city = "杭州市"
// 执行实时天气信息查询
weather.getLive(city, function(district, data) {
const weather = data.weather
console.log(data)
const count = weatherList.length
for (let i = 0; i < count; i++) {
// console.log(weatherList[i])
if (weatherList[i].name === data.weather) {
weaterImgUrl.value = weatherList[i].url
break
}
}
// console.log(data)
})
}).catch(e => {
console.log(e)
})


onMounted(() => {
console.log("sdfa")
})
</script>
<style lang="less" scoped>
.weater-img{
width:100%;
height:100%;
vertical-align: middle;
}

</style>
文章目录
小额赞助
本人提供免费与付费咨询服务,感谢您的支持!赞助请发邮件通知,方便公布您的善意!
**光 3.01 元
Sun 3.00 元
bibichuan 3.00 元
微信公众号
广告位
诚心邀请广大金主爸爸洽谈合作
每日一省
isNaN 和 Number.isNaN 函数的区别?

1.函数 isNaN 接收参数后,会尝试将这个参数转换为数值,任何不能被转换为数值的的值都会返回 true,因此非数字值传入也会返回 true ,会影响 NaN 的判断。

2.函数 Number.isNaN 会首先判断传入参数是否为数字,如果是数字再继续判断是否为 NaN ,不会进行数据类型的转换,这种方法对于 NaN 的判断更为准确。

每日二省
为什么0.1+0.2 ! == 0.3,如何让其相等?

一个直接的解决方法就是设置一个误差范围,通常称为“机器精度”。对JavaScript来说,这个值通常为2-52,在ES6中,提供了Number.EPSILON属性,而它的值就是2-52,只要判断0.1+0.2-0.3是否小于Number.EPSILON,如果小于,就可以判断为0.1+0.2 ===0.3。

每日三省
== 操作符的强制类型转换规则?

1.首先会判断两者类型是否**相同,**相同的话就比较两者的大小。

2.类型不相同的话,就会进行类型转换。

3.会先判断是否在对比 null 和 undefined,是的话就会返回 true。

4.判断两者类型是否为 string 和 number,是的话就会将字符串转换为 number。

5.判断其中一方是否为 boolean,是的话就会把 boolean 转为 number 再进行判断。

6.判断其中一方是否为 object 且另一方为 string、number 或者 symbol,是的话就会把 object 转为原始类型再进行判断。

每日英语
Happiness is time precipitation, smile is the lonely sad.
幸福是年华的沉淀,微笑是寂寞的悲伤。