微信小程序获取当前位置(二)
现在要点击下面图标的时候获取到当前位置信息:
wxml
<view class="task-item">
<view class="task-item-title">详细地址</view>
<view class="task-item-input2">
<input name='address' placeholder="请输入详细地址" value="{
{address}}" type="text" bindinput="getAddress" placeholder-class='rightinput'></input>
<view class="place" bindtap='choosePlace'>
<image src="/images/icon/place.png" ></image>
</view>
</view>
</view>
js
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//授权
var that=this;
wx.getLocation({
type:"wgs84",
success:res=>{
that.setData({
latitude:res.latitude,
longitude:res.longitude
})
}
})
},
choosePlace(e) {
let that = this
console.log(e);
var obj = e.target.dataset;
wx.getSetting({
success(res) {
console.log(res)
if (!res.authSetting['scope.userLocation']) {
wx.openSetting({
success(res) {
console.log(res.authSetting)
}
})
} else {
wx.chooseLocation({
success: ces => {
console.log(ces)
that.setData({
address: ces.address
})
}
})
}
}
});
},
点击出现当前位置:
手机上真机看定位准确性会高一些。
还没有评论,来说两句吧...