1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 小程序获取城市经纬度_微信小程序demo:天气预报 根据经纬度获取城市 获取天气...

小程序获取城市经纬度_微信小程序demo:天气预报 根据经纬度获取城市 获取天气...

时间:2019-12-30 12:30:25

相关推荐

小程序获取城市经纬度_微信小程序demo:天气预报 根据经纬度获取城市 获取天气...

[AppleScript] 纯文本查看 复制代码//app.js

Page({

data: {

city: "",

today: {},

future:{}

},

onLoad: function (options) {

// 生命周期函数--监听页面加载

this.loadInfo();

},

loadInfo: function () {

var page = this;

//获取经纬度

wx.getLocation({

type: 'gcj02',

success: function (res) {

// success,获取当前的城市信息

var latitude = res.latitude

var longitude = res.longitude

console.log(latitude);

page.getCity(latitude, longitude)

},

fail: function () {

// fail

},

complete: function () {

// complete

}

})

},

//根据经纬度获取城市

getCity: function (latitude, longitude) {

var page = this;

wx.request({

url: 'http://apis./ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=EFHBZ-WQG6U-WEMVB-4N3DG-KSMGT-5WB2G',

header: {

'content-type': 'application/json'

},

success: function (res) {

var city = res.data.result.address_component.city;

console.log(city);

//把市去掉,下一个接口地址没有模糊处理

city = city.replace("市", "");

page.setData({ city: city });

page.getWeather(city);

}

})

},

//根据城市获取天气信息

getWeather: function (city) {

var page = this;

wx.request({

url: '/weather_mini?city=' + city,

header: {

'content-type': 'application/json'

},

success: function (res) {

console.log(res);

var future = res.data.data.forecast;

//移除掉数组中当天的天气信息

var todayInfo = future.shift();

var today = res.data.data;

today.todayInfo = todayInfo;

page.setData({ today: today, future: future });

},

})

},

})

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。