关于获取用户当前位置,微信官方文档使用wx.getLocation()来让用户授权位置信息wx.getLocation({type:'wgs84',//默认为wgs84返回gps坐标,gcj02返回可用于wx.openLocation的坐标success:function(res){varlatitude=res.latitudevarlongitude=res.longitudeconsole.log(res)}})得到输出的结果这时候需要将地理坐标信息转化为具体中文位置腾讯官方给出了针对小程序地图位置信息的JavaScriptSDK第一步,申请密钥第二步,通过逆地址解析,就可以实现地理位置的中文转换。最终输出结果js代码//引入SDK核心类varQQMapWX=require('../../tools/qqmap-wx-jssdk.min.js');//实例化API核心类vardemo=newQQMapWX({key:'开发者密钥'//必填});Page({data:{localCity:""//本地城市}/生命周期函数--监听页面加载/onLoad:function(options){letthat=this;//调用接口wx.getLocation({type:'wgs84',success:function(res){varlatitude=res.latitude//纬度varlongitude=res.longitude//经度demo.reverseGeocoder({location:{latitude:latitude,longitude:longitude},success:function(res){console.log(res);letprovince=res.result.address_component.province;//省份letcity=res.result.address_component.city;//城市that.setData({localCity:city})},fail:function(res){console.log(res);}});}})}})

TAG标签: 微信小程序小程序微信模拟定位

http://www.xiaochengxub.com/read-108068.html