zoukankan      html  css  js  c++  java
  • 微信小程序位置

    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)
                  }
                })
               }
             }
            })
           }
         }
       })
      },
  • 相关阅读:
    列表左边左右固定,右边可以左右滚动,且左右两边列表滚动时上下联动
    SQL分组多列统计(GROUP BY后按条件分列统计)
    C# 调用webservice 几种办法(转载)
    解析XML文档
    GDI+中发生一般性错误的解决办法
    所闻所获2:使用块回调来实现代理的功能
    OC基础11:基本的C语言特性2
    OC基础10:基本的C语言特性1
    OC基础9:预处理程序
    OC基础8:分类和协议
  • 原文地址:https://www.cnblogs.com/shanchui/p/13379199.html
Copyright © 2011-2022 走看看