当前位置: 首页 > Web前端 > HTML5

小程序获取定位天气(百度地图)

时间:2023-04-05 22:54:40 HTML5

小程序调用百度地图API创建百度应用在百度地图开放平台控制台创建应用![image.png](/img/bVbBVkP)记得绑定真实的APPID,会生成访问应用的AK如何使用设置小程序请求合法域名(服务域名)导入百度apiSDK-wx.js官网有资源//参考百度地图微信小程序JSAPI模块varbmap\=require('../../libs/bmap-wx.js');Page({globalData:{BMap:null,baiduKey:'uKYgmr*********************Hq3',//你的AK地址Name:null,//地址currentWeather:{//Weather},},onLaunch:function(){varthat\=this;//新建一个百度地图对象that.globalData.BMap=newbmap.BMapWX({ak:that.globalData.baiduKey});},onShow:function(){varthat=thisthatthat.globalData.BMap.regeocoding({iconPath:'../../static/icon-logo.png',iconTapPath:'../../static/icon-logo.png',成功:(数据)=>{this.globalData.addressName=data.wxMarkerData[0].address}})that.globalData.BMap.weather({成功:(数据)=>{let{currentWeather,originalData}=dataletweatherObj={}//这是格式化后的数据,不用引用lethour=newDate().getHours()//判断是白天还是黑夜weatherObj.currentCity=当前天气[0]。currentCity//城市weatherObj.weatherDesc=currentWeather[0].weatherDesc//天气描述weatherObj.date=currentWeather[0].date.split("(")[0]//时间weatherObj.temperature=currentWeather[0].date.split("(")[1].split(")")[0].split(":")[1].split("℃")[0]//实时温度weatherObj.pictureUrl=小时>19&&小时<7?originalData.results[0].weather_data[0].nightPictureUrl:originalData.results[0].weather_data[0].dayPictureUrlthis.globalData.currentWeather=weatherObj;console.log(weatherObj)}})}})接下来是页面展示,这里只是一个简单的应用,如果你有复杂的需求,或者使用地图应用,你可以如果可能,我们将继续深入研究更新。感谢您的好评,转载并注明出处

最新推荐
猜你喜欢