<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>获取当天地区与天气信息</title>
<script src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2&ak=hLqdNZAF6tHexI8jQGshBBjNhgYZXHPv"></script>
<script>
$(function () {
getNowArea()
})
/* 定位当前城市 */
function getNowArea() {
let nowCity = new BMap.LocalCity();
nowCity.get(result => {
let cityName = result.name; //当前的城市名
console.log(cityName);
refreshWeather(cityName);
});
}
/* 获取天气信息 */
function refreshWeather(cityName) {
jQuery.support.cors = true;
let url = encodeURI("http://wthrcdn.etouch.cn/weather_mini?city=" + cityName);
$.ajax({
url: url,
type: "get",
dataType: "json",
async: false,
success: function (data) {
console.log('天气', data)
}
});
}
</script>
</head>
<body>
</body>
</html>
通过js获取当天地区与天气信息
最新推荐文章于 2024-07-04 16:48:50 发布