小程序获取当时的位置且具体展示以及相关的天气温度

发布时间 2024-01-08 11:14:08作者: 星宝攸宁

项目的需求是需要在小程序中展示到你所在的位置的具体市、区、街道甚至门牌号等。

此处我用uni-app中的uni.getLocation与腾讯地图的逆地址解析结合,获取相关地址。如悦来广场国贸大厦等。

腾讯文档地址:https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder

详细重点代码:

uni.getLocation({
    type: 'gcj02',
    success: function(res) {
        var lon2 = res.longitude;   //经度
        var lat2 = res.latitude;    //维度
        console.log(lon2, lat2, '==```')
        var lo = lat2 + ',' + lon2
        uni.request({
            url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' +
                lo +
                '&key=这里是你腾讯申请的key&get_poi=0', //仅为示例,并非真实接口地址。
            success: (res) => {
                console.log('这是打印出来的具体信息,根据实际情况做出展示',res.data.result);
            }
        });
    }
});

这样就能做具体的展示了。

温度天气等我用的高德天气免费接口,具体代码如下:

let that = this;
        uni.request({
            url: 'https://restapi.amap.com/v3/weather/weatherInfo', //仅为示例,并非真实接口地址。
            data: {
                city: 370500,
                key: "应用的key",
                
            },
            success: (response) => {
                let weatherInfo = response.data.lives[0].weather;
                let temperature = response.data.lives[0].temperature;
                // console.log(weatherInfo);
                that.weather = `${weatherInfo}    ${temperature}  °C`;
                // console.log('嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎',response,that.weather);
            }
        });