日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

小程序 获取当前用户地址及地图显示

發布時間:2023/12/9 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 小程序 获取当前用户地址及地图显示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

步驟

  • 使用 wx.getLocation來獲取當前位置:
    注意;當用戶取消位置獲取授權之后,再次點擊獲取位子按鈕小程序不會再提醒用戶是否授權,這個時候最好自己彈出提示框讓用戶去設置頁面開啟授權設置.
  • wx.getLocation({type: 'wgs84', //wgs返回 gps坐標, gcj02返回可用于wx.openLocation 的坐標altitude: 'false' //true 會返回高度信息,由于獲取高度需要較高精確度,會減慢接口返回速度success (res) { //接口調用成功的回調函數const latitude = res.latitude // 緯度,范圍為 -90~90,負數表示南緯const longitude = res.longitude // 經度,范圍為 -180~180,負數表示西經const speed = res.speed // 速度,單位 m/sconst accuracy = res.accuracy // 位置的精確度const altitude = res.altitude // 高度,單位 m...},fail (err) { //接口調用失敗的回調函數},complete(){} }) 注意: 工具中定位模擬使用IP定位,可能會有一定誤差。且工具目前僅支持 gcj02 坐標。 使用第三方服務進行逆地址解析時,請確認第三方服務默認的坐標系,正確進行坐標轉換。 (后面需要使用獲取到的數據打開微信內置地圖,最好使用gjc02)
  • 使用 wx.openLocation打開地址位置:
    將經緯度傳進去就可以打開地圖并標記當前位置.
  • wx.getLocation({type: 'gcj02', //返回可以用于wx.openLocation的經緯度success (res) {const latitude = res.latitudeconst longitude = res.longitudewx.openLocation({latitude,longitude,scale: 18})} }) 微信公眾平臺API:https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.openLocation.html
  • 使用wx.chooseLocation來打開地圖選擇位置.(建議在手機上調試)
  • wx.getLocation({type: 'gcj02', //返回可以用于wx.openLocation的經緯度success (res) {const latitude = res.latitudeconst longitude = res.longitudewx.openLocation({latitude,longitude,wx.chooseLocation({success: function(res) {console.log('定位成功')},})})} })

    實例

    <button bindtap="getLocation">獲取當前地址</button>getLocation () {let _this = thiswx.getLocation({type: 'gcj02',success(res) {console.log('獲取地理經緯度成功' + res)_this.setData({latitude: res.latitude,longitude: res.longitude})wx.openLocation({latitude: res.latitude,longitude: res.longitude,scale: 18,success(res){console.log('打開地址位置成功')wx.chooseLocation({success: function(res) {console.log('定位成功')},fail(err){console.log('定位失敗' + err)}})}})},fail(err){console.log(err)}})},缺點:地獄回調,建議異步




    缺陷:
    使用 wx.getLocation來獲取當前位置時:當用戶使用 wx.getLocation來獲取當前位置,取消位置獲取授權之后,再次點擊獲取位子按鈕小程序不會再提醒用戶是否授權,這個時候最好自己彈出提示框讓用戶去設置頁面開啟授權設置
    解決辦法: 添加用戶是否初次進入該頁面以及是否地理位置授權的判斷
    代碼:

    wx.getSetting({success: (res) => {console.log(JSON.stringify(res))// res.authSetting['scope.userLocation'] == undefined 表示 初始化進入該頁面// res.authSetting['scope.userLocation'] == false 表示 非初始化進入該頁面,且未授權// res.authSetting['scope.userLocation'] == true 表示 地理位置授權if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {// 非初始化進入該頁面 && 未授權wx.showModal({ // 彈出提示框讓用戶去設置頁面開啟授權設置title: '請求授權當前位置',content: '需要獲取您的地理位置,請確認授權',success (res) {if (res.cancel) {wx.showToast({title: '拒絕授權',icon: 'none',duration: 1000})} else if (res.confirm) {wx.openSetting({success (dataAu) {if (dataAu.authSetting["scope.userLocation"] == true) {wx.showToast({title: '授權成功',icon: 'success',duration: 1000})//再次授權,此處省略調用wx.getLocation的API} else {wx.showToast({title: '授權失敗',icon: 'none',duration: 1000})}}})}}})} else if (res.authSetting['scope.userLocation'] == undefined) {//初次進入該頁面,此處省略調用wx.getLocation的API}else {// 地理位置授權成功 ,此處省略調用wx.getLocation的API}}})

    總結

    以上是生活随笔為你收集整理的小程序 获取当前用户地址及地图显示的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。