获取位置,天气温度

<template>
    <view>
        <!-- 定位城市 -->
        <view class="addr">{{ city }}</view>
        <!-- 温度 -->
        <view class="temperature">{{ temperature }}℃</view>
    </view>
</template>
<script>
//高德SDKjs
import amap from '@/common/SDK/amap-wx.js';
export default {
    data() {
        return {
            key:'自己申请的key',//小程序定位key
            webkey:'自己申请的key',//获取天气key,key的服务不同所以用两个
            city: '',//城市
            temperature:''//气温
        };
    },
    methods:{
        tian(e){
              uni.request({
                  url: 'https://restapi.amap.com/v3/weather/weatherInfo',//高德地图查询天气
                  method :'GET',
                  data:{
                      key:this.webkey,
                      city:e,
                       },
                  success: (res) => {
                      this.temperature = res.data.lives[0].temperature
                      console.log(this.temperature)
                  }
              });
        }
    },
    onLoad() {
        this.amapPlugin = new amap.AMapWX({
            //高德地图小程序KEY,替换为自己的KEY,参考:http://ask.dcloud.net.cn/article/35070
                        //https://lbs.amap.com/api/wx/gettingstarted
            key: this.key
        });
        //定位地址
        this.amapPlugin.getRegeo({
            success: (res)=> {
                //this.city = data[0].regeocodeData.addressComponent.city.replace(/市/g, ''); //把"市"去掉
                this.city = res[0].regeocodeData.addressComponent.city; 
                let adData = res[0].regeocodeData.addressComponent.adcode//拿到城市的编码用于查寻天气
                //使用说明https://lbs.amap.com/api/webservice/guide/api/weatherinfo/#instructions
                console.log( adData) 
                 this.tian(adData)
            }
        });
    }
}
</script>
<style>



</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值