微信小游戏里面获取当前位置,显示天气信息

微信小游戏里面获取当前位置,显示天气信息

通过微信 API wx.getLocation 获取当前位置的纬度和经度

在这里插入图片描述
在这里插入图片描述

示例代码:

wx.getLocation({
  type: 'wgs84',
  success: function(res) {
    const latitude = res.latitude
    const longitude = res.longitude
    const speed = res.speed
    const accuracy = res.accuracy
  }
})

通过当前位置的纬度和经度在百度api获取当前位置信息

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

示例代码:

let xhr1 = new XMLHttpRequest();
let url1 = `http://api.map.baidu.com/geocoder/v2/?ak=r49Gt5U1GlgZVYOa6IoEMqZBQTHrLkYG&location=${latitude},${longitude}&output=json&pois=1`;
xhr1.onreadystatechange = function () {
    if (xhr1.readyState == 4 && (xhr1.status >= 200 && xhr1.status < 400)) {
            let response1 = xhr1.responseText;
                    let result = JSON.parse(response1);
                    console.log(response1);
                    console.log(result.status);
                    console.log(result.result);
                    city = result.result.addressComponent.city;
                    console.log('省区:'+city);
                    district = result.result.addressComponent.district;
                    console.log('县区:'+ district);
                }
};
xhr1.open("GET", url1, true);
xhr1.send(); 

##通过中国万年历天气接口和城市信息获取天气信息

示例代码:

let xhr = new XMLHttpRequest();
let url = `http://wthrcdn.etouch.cn/weather_mini?city=${district}`;
xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
        let response = xhr.responseText;
        console.log(response);
        let weatherInfo = JSON.parse(response);
        console.log(weatherInfo.data.city);
        console.log(weatherInfo.data.forecast);
        forecast = weatherInfo.data.forecast;
    }
};
xhr.open("GET", url, true);
xhr.send(); 

##结果图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值