Code for weather forecast development in WeChat applet
This article mainly introduces the relevant information of the WeChat Mini Program Weather Forecast development example code source code. The source code is included here. Friends in need can refer to it
WeChat Mini Program Weather Forecast
Main functions of the example
Automatically locate the city
Get weather information based on the located city
Display the weather conditions for the next few days
View detailed weather information for the day
Look at the renderings first
WeChat Mini Program-Weather Home Page
WeChat Mini Program-Weather Details Page
Ideas and Coding Department Automatically locate the city
wx.getLocation: Through the API of the official documentation, you can see that wx.getLocation can obtain the current geographical location and speed, but the obtained geographical location is only Longitude and latitude, not the real city name, but we can obtain the city name and other information based on this longitude and latitude (need to use a third-party interface), and then obtain the corresponding weather information through the city name and city ID.
Add functions in the .js logic layer:
data:{ weatherApikey:'', //天气apikey,在http://apistore.baidu.com 上申请 city:'', //城市名称 areaid:'', //城市对应的id curWd:{}, //当天天气情况 indexs:{}, //当天天气详情说明 forecast:{} //未来4天的天气情况 }, onLoad:function(options){ // 生命周期函数--监听页面加载 this.setData({weatherApikey:getApp().globalData.weatherApikey}); this.loadLocation(); }, //获取当前的位置信息,即经纬度 loadLocation: function() { var page = this; wx.getLocation({ type: 'gcj02', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标 success: function(res){ // success var latitude = res.latitude; var longitude = res.longitude; //获取城市 page.loadCity(latitude, longitude); } }) }, //通过经纬度获取城市 loadCity: function(latitude, longitude) { var page = this; //这个key是自己在http://apistore.baidu.com上申请的 var key = "XSWBZ-EVQ3V-UMLPA-U4TP6-6MQFZ-UUFSL"; var url = "http://apis.map.qq.com/ws/geocoder/v1/?location="+latitude+","+longitude+"&key="+key+"&get_poi=1"; wx.request({ url: url, data: {}, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT // header: {}, // 设置请求的 header success: function(res){ // success var city = res.data.result.address_component.city; city = city.replace("市", ""); //将“市”去掉,要不然取不了天气信息 page.setData({city: city}); page.loadId(city); } }) }, //通过城市名称获取城市的唯一ID loadId: function(city) { var page = this; var url = "http://apis.baidu.com/apistore/weatherservice/citylist"; wx.request({ url: url, data: { cityname: city }, header: { apikey:page.data.weatherApikey }, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT success: function(res){ // success var cityid = res.data.retData[0].area_id; page.setData({areaid: cityid}); page.loadWeather(city, cityid); } }) }, //通过城市名称和城市ID获取天气情况 loadWeather: function(city, areaId) { var page = this; var url = "http://apis.baidu.com/apistore/weatherservice/recentweathers"; wx.request({ url: url, data: { cityname:city, cityid: areaId }, header: { apikey: page.data.weatherApikey }, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT success: function(res){ // success page.setData({curWd : res.data.retData.today, indexs: res.data.retData.today.index, forecast:res.data.retData.forecast}); } }) }, //事件绑定,跳转到天气详情页面 gotoDetail: function(event) { // console.log(this.data.areaid+"==在这里跳转=="+this.data.city); wx.navigateTo({ url: '../detail/detail?city='+this.data.city+"&cityid="+this.data.areaid }) }
##Note: page.setData or this.setData are both Used to set the data value in data. From the above logic layer, we can see that here we basically process data and some event bindings, and WeChat itself has encapsulated many practical functions for us, such as: wx.navigateTo, wx.request, wx. getLocation is somewhat similar to AngularJS's two-way data binding when communicating with the view.
index.wxml parsing<view class="main-container"> <import src="../templates/today-tpl"/> <view bindtap="gotoDetail"> <template is="today-tpl" data="{{city, curWd}}"/> </view> <import src="../templates/index-tpl"/> <view class="index-content"> <block wx:for="{{indexs}}" wx:key="item" wx:for-index="idx"> <template is="index-tpl" data="{{item,idx}}"></template> </block> </view> <import src="../templates/forecast-tpl"/> <view class="forecast"> <block wx:for="{{forecast}}" wx:key="item"> <template is="forecast-tpl" data="{{item}}"/> </block> </view> </view>
Template file
The template file is actually a wxml file<template name="today-tpl"> <view class="today"> <view class="city">{{city}}</view> <view class="date">{{curWd.date}} {{curWd.week}}</view> <view class="temp">{{curWd.curTemp}}</view> <view class="weather">{{curWd.type}} {{curWd.lowtemp}}/{{curWd.hightemp}}</view> <view class="wd">{{curWd.wd}}</view> </view> </template>
WeChat applet implements skin’s night mode
Implementation of the shopping cart function in WeChat applet
Implementation of novel reading applet in WeChat applet
The above is the detailed content of Code for weather forecast development in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Xianyu's official WeChat mini program has quietly been launched. In the mini program, you can post private messages to communicate with buyers/sellers, view personal information and orders, search for items, etc. If you are curious about what the Xianyu WeChat mini program is called, take a look now. What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3. If you want to use it, you must activate WeChat payment before you can purchase it;

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

Since December 2021, Huawei & Honor mobile phones have launched the Vientiane desktop widget function. Many convenient functions, visually optimized desktop controls, etc. have been added to many users’ mobile desktops; by August this year, the two major merchant platforms also opened up sports and health data, weather data, music data, system data, etc., allowing users to use their mobile desktops The interactive operation is more convenient, faster and more interesting, allowing users to DIY and create their own personalized desktop. Mobile desktop after adding widgets Recently, many Huawei mobile phone users have reported that they are not clear about how to add desktop widgets on Huawei and Honor mobile phones, complaining that the process is too complicated and cumbersome. In order to help everyone solve this problem, Qian Shuxian has prepared a detailed operation process, hoping to

To implement the drop-down menu effect in WeChat Mini Programs, specific code examples are required. With the popularity of mobile Internet, WeChat Mini Programs have become an important part of Internet development, and more and more people have begun to pay attention to and use WeChat Mini Programs. The development of WeChat mini programs is simpler and faster than traditional APP development, but it also requires mastering certain development skills. In the development of WeChat mini programs, drop-down menus are a common UI component, achieving a better user experience. This article will introduce in detail how to implement the drop-down menu effect in the WeChat applet and provide practical

Implementing picture filter effects in WeChat mini programs With the popularity of social media applications, people are increasingly fond of applying filter effects to photos to enhance the artistic effect and attractiveness of the photos. Picture filter effects can also be implemented in WeChat mini programs, providing users with more interesting and creative photo editing functions. This article will introduce how to implement image filter effects in WeChat mini programs and provide specific code examples. First, we need to use the canvas component in the WeChat applet to load and edit images. The canvas component can be used on the page

Use the WeChat applet to achieve the carousel switching effect. The WeChat applet is a lightweight application that is simple and efficient to develop and use. In WeChat mini programs, it is a common requirement to achieve carousel switching effects. This article will introduce how to use the WeChat applet to achieve the carousel switching effect, and give specific code examples. First, add a carousel component to the page file of the WeChat applet. For example, you can use the <swiper> tag to achieve the switching effect of the carousel. In this component, you can pass b

How to use JavaWebSocket to implement real-time weather forecast function? With the popularity of the Internet and mobile devices, real-time weather forecast function has become one of the essential functions of many applications. Using JavaWebSocket technology can realize real-time communication conveniently and quickly, providing users with the latest weather forecast information. This article will introduce how to use JavaWebSocket to implement the real-time weather forecast function and provide specific code examples. Environment preparation Before starting, you need to make sure that you have installed

The official WeChat mini program of Xianyu has been quietly launched. It provides users with a convenient platform that allows you to easily publish and trade idle items. In the mini program, you can communicate with buyers or sellers via private messages, view personal information and orders, and search for the items you want. So what exactly is Xianyu called in the WeChat mini program? This tutorial guide will introduce it to you in detail. Users who want to know, please follow this article and continue reading! What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3.
