微信小程序获取当前位置(二)

女爷i 2022-12-20 12:55 396阅读 0赞

现在要点击下面图标的时候获取到当前位置信息:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FzdGVyaWFW_size_16_color_FFFFFF_t_70

wxml

  1. <view class="task-item">
  2. <view class="task-item-title">详细地址</view>
  3. <view class="task-item-input2">
  4. <input name='address' placeholder="请输入详细地址" value="{
  5. {address}}" type="text" bindinput="getAddress" placeholder-class='rightinput'></input>
  6. <view class="place" bindtap='choosePlace'>
  7. <image src="/images/icon/place.png" ></image>
  8. </view>
  9. </view>
  10. </view>

js

  1. /**
  2. * 生命周期函数--监听页面加载
  3. */
  4. onLoad: function (options) {
  5. //授权
  6. var that=this;
  7. wx.getLocation({
  8. type:"wgs84",
  9. success:res=>{
  10. that.setData({
  11. latitude:res.latitude,
  12. longitude:res.longitude
  13. })
  14. }
  15. })
  16. },
  17. choosePlace(e) {
  18. let that = this
  19. console.log(e);
  20. var obj = e.target.dataset;
  21. wx.getSetting({
  22. success(res) {
  23. console.log(res)
  24. if (!res.authSetting['scope.userLocation']) {
  25. wx.openSetting({
  26. success(res) {
  27. console.log(res.authSetting)
  28. }
  29. })
  30. } else {
  31. wx.chooseLocation({
  32. success: ces => {
  33. console.log(ces)
  34. that.setData({
  35. address: ces.address
  36. })
  37. }
  38. })
  39. }
  40. }
  41. });
  42. },

点击出现当前位置:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FzdGVyaWFW_size_16_color_FFFFFF_t_70 1

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FzdGVyaWFW_size_16_color_FFFFFF_t_70 2

手机上真机看定位准确性会高一些。

发表评论

表情:
评论列表 (有 0 条评论,396人围观)

还没有评论,来说两句吧...

相关阅读