发表日期:2017-04-06 10:23:16编辑:武汉新度网络科技有限公司 标签: 微信小程序,江夏建站,江夏做网站,江夏网站建设,武汉网站建设
微信交流群 QQ群号:450767502
扫描二维码进群(进群就可以查看小程序开发视频教程 ,我们定期会发布一些小程序前端案例)

在上章我们讲到了怎么获取用户信息和缓存用户信息
在这章我们将讲到如果获取和缓存当前用户所在的省、市、区信息
首先我们要了解小程序 获取当前的地理位置的api
wx.getLocation(OBJECT)获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用;当用户点击“显示在聊天顶部”时,此接口可继续调用。
示例代码:
wx.getLocation({
type: 'wgs84',
success: function(res) { var latitude = res.latitude var longitude = res.longitude var speed = res.speed var accuracy = res.accuracy
}
})然后我们还需要了解小程序 设置缓存的api
wx.setStorageSync(KEY,DATA)
将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。
示例代码:
try {
wx.setStorageSync('key', 'value')
} catch (e) {
}下面我们举出实际的例子
//获取纬度,经度
wx.getLocation({
type: 'wgs84',
success: function (res) {
var latitude = res.latitude
var longitude = res.longitude
wx.request({
url: app.url + 'data.php?action=get_city',
data: {
latitude: latitude,
longitude: longitude
},
headers: {
'Content-Type': 'application/json'
},
success: function (res) {
//console.log(res.data)
var province = res.data.result.addressComponent.province
//console.log(province)
var city = res.data.result.addressComponent.city
var district = res.data.result.addressComponent.district
var diqu = province+city+district
//缓存当前所在地区
wx.setStorageSync('dq_diqu', diqu)
wx.setStorageSync('dq_district', district)
}
})
}
})在data.php文件中接受请求
if($act=="get_city"){
//获取当然城市
//http://api.map.baidu.com/geocoder/v2/?ak=327381a342077a8f3d584251b811cce5&callback=renderReverse&location=30.593099,114.305393&output=json
//纬度
$latitude = $_REQUEST['latitude'];
//经度
$longitude = $_REQUEST['longitude'];
$url = 'http://api.map.baidu.com/geocoder/v2/?ak=327381a342077a8f3d584251b811cce5&location='.$latitude.','.$longitude.'&output=json';
$result = file_get_contents($url);
exit($result);
}
这样地理信息就基本获取到了,如果还有不清楚的地方可以QQ咨询1987835510专业顾问
全天24小时随时沟通
服务贴心、周到
随时随地回答您的问题
提供线上支付
方便、快捷更可靠
强大售后团队
一流服务,无后顾之忧