• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

微信小程序位置

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

wx.openLocation  获取定位打开地图,配合wx.getLocation得到的经纬度实现定位,不能单独使用

wx.chooseLocation  根据已有的经纬度打开地图

 

<view class="location-1" bindtap="open_location_1">
  定位
</view>
<view class="location-2"  bindtap="open_location_2">定位2</view>
 

 

 

 

 

 open_location_1(){
    console.log("我被点击了")

    wx.getSetting({
      success:res=>{
        console.log(res)
        if(!res.authSetting["scope.userLocation"]){
          // 未开启定位
          wx.showModal({
            title:\'提示\',
            content:\'我们需要获取您的位置信息,为您提供更多的精彩内容\',
            confirmText:"授权",
            cancelText:"取消",
            success:res=>{
              if(res.confirm){
                wx.openSetting({
                  success:res=>{
                    wx.getLocation({
                      type:\'gcj02\',
                      success:res=>{
                        console.log(res)
                        wx.openLocation({
                          latitude: res.latitude,
                          longitude:res.longitude,
                          success:res=>{
                            console.log(\'成功\',res)
                          }
                        })
                      }
                    })
                  }
                })
              }else{
                wx.showToast({
                  title: \'您拒绝了授权,请在设置中开启授权定位\',
                })
              }
            }
          })
        }else{
         wx.getLocation({
            type:\'gcj02\',
            success:res=>{
              console.log(res)
              wx.openLocation({
                latitude: res.latitude,
                longitude:res.longitude,
                success:res=>{
                  console.log(\'成功\',res)
                }
              })
            }
          })
        }
      }
    })
    
  },

 

注意使用wx.getLocation需要在app.json中加上这个属性

"permission": {
    "scope.userLocation": {
      "desc": "地图选点需获取您的实时位置"
    }
  },

 

 

 open_location_2(){
   var latitude= 31.82057
   var longitude= 117.22901

   wx.chooseLocation({
     latitude:latitude,
     longitude:longitude,
     success:res=>{
       console.log(res)
     },fail:err=>{
       if(err.errMsg=="chooseLocation:fail auth deny"){
        wx.openSetting({
         success:res=>{
           console.log(res)
           if(!res.authSetting["scope.userLocation"]){
             wx.showToast({
               title: \'你拒绝了授权,请在设置中自行授权定位\',
             })
           }else{
            wx.chooseLocation({
              latitude:latitude,
              longitude:longitude,
              success:res=>{
                console.log(res)
              }
            })
           }
         }
        })
       }
     }
   })
  },

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
微信小程序获取地理位置授权发布时间:2022-07-18
下一篇:
微信小程序API~地理位置location发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap