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

主动打开手机定位功能

tigon4年前 (2021-08-19)小程序3156
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

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

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

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

小程序自定义下拉刷新

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

小程序多层展示收起

小程序多层展示收起

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

小程序页面开启下拉刷新不回弹问题

onPullDownRefresh: function () { // 停止下拉动作 wx.stopPullDownRefresh(); //添加这句就可以了 if(this.data.searchContent){ this.setSearchListDataFn(th...

小程序接地图

小程序接地图

longitude latitude是中心经纬度markers 是所有打点的经纬度,微信地图也是包了高德地图 <map wx:if="{{mapShow}}" class="map" id="map" scale="...

小程序分享功能

小程序分享功能

小程序分享功能 /** * 用户点击右上角分享 */ onShareAppMessage: function () { return { title: this.data.goodsInfo.goods_name, path: `/...