当前位置:首页 > 小程序 > 正文内容

主动打开手机定位功能

tigon4年前 (2021-08-19)小程序3009
export function getLocationListener(){
  return new Promise((resolve, reject) => {
    var that = this
    wx.openSetting({
      success(res){
        if (res.authSetting["scope.userLocation"] === true) {
          // wx.showToast({
          //   title: '授权成功',
          //   icon: 'none',
          //   duration: 1000
          // })
          wx.getLocation({
            isHighAccuracy: true,
            success(res) {
              resolve(res)
            },
          });
        } else {
          wx.showToast({
            title: '授权失败',
            icon: 'none',
            duration: 1000
          })
          wx.navigateBack({
            delta: -1
          });
          reject(false)
        }
      },
      fail: function () {
        reject(false)
      }
    })
  })
}

const res = await getLocationListener()
    this.setData({
      flag:true,
      ['queryGoodsInfo.lat']: res.latitude,
      ['queryGoodsInfo.lon']: res.longitude
    })

定位代码根据需求修改使用

export function locationListener(){
  return new Promise((resolve, reject) => {
    wx.getLocation({
      success(res) {
        resolve(res)
      },
      fail() {
        wx.getSetting({
          success(res) {
            var statu = res.authSetting;
            if (!statu['scope.userLocation']) {
              resolve(true)
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '授权失败',
              icon: 'none',
              duration: 1000
            })
            reject(false)
          }
        })
      }
    })
  })
}

版权声明:本文由Web学习之路发布,如需转载请注明出处。

本文链接:https://webge.net/?id=131

“主动打开手机定位功能” 的相关文章

小程序 bind与catch 区别

bind和catch后可以紧跟一个冒号,+事件分类例如:写法:1,bindtap="函数名"2,bind:tap="函数名"  这里这个tap可以换成需要的事件 双击,单击,滑动之类的bind是带有冒泡的catch不带有冒泡...

小程序-- wx.switchTab({url}) 跳转页面

wx.switchTab({url}) 传页面page地址 注意,页面会刷新,那下面导航如何选中呢? 1,在app.js里定义全局变量 globalData: { selected: 0, }, 2,每个页面引入全局变量 getApp().globalData.se...

小程序如果以打包浏览的时候太大怎么处理---分包

小程序如果以打包浏览的时候太大怎么处理---分包

小程序如果以打包浏览的时候太大怎么处理—分包 类似这样的目录来分包处理 app.json需要做分包路径处理 当如果用跳转路径分包路径的时候例如...

小程序自定义下拉刷新

<scroll-view style='height:100%' scroll-y='{{!isindrag}}' bindscroll='scorll'> <view class='column' bindtouc...

小程序列表展开收起功能

小程序列表展开收起功能

index.wxml <block wx:for-items="{{list}}" wx:key="{{item.id}}"> <view class="kind-list-item"> <...

小程序多层展示收起

小程序多层展示收起

<view class='list_box' wx:for='{{list}}' wx:key='this' wx:for-item='parentItem' wx:for-index='parentIndex'...