日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
微信小程序怎么獲取當前位置和城市名-創(chuàng)新互聯(lián)

小編給大家分享一下微信小程序怎么獲取當前位置和城市名,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)是一家以重慶網(wǎng)站建設公司、網(wǎng)頁設計、品牌設計、軟件運維、seo優(yōu)化、小程序App開發(fā)等移動開發(fā)為一體互聯(lián)網(wǎng)公司。已累計為成都社區(qū)文化墻等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務。

   1, 獲取當前地理位置,首先要拿到用戶的授權wx.openSetting;

    2,微信的getLocation接口,獲取當前用戶的地理位置(微信返回的是經(jīng)緯度,速度等參數(shù));

    3,微信沒有將經(jīng)緯度直接轉(zhuǎn)換為地理位置,借用騰訊位置服務中關于微信小程序的地理轉(zhuǎn)換JS SDK 的API(返回信息中包括國家,省,市,區(qū),經(jīng)緯度等地理位置)
步驟描述清楚以后,下面就開始按步驟操作了;(本文僅僅講述如何獲取用戶地理位置的授權)

圖示為獲取用戶地理位置授權彈窗

微信小程序怎么獲取當前位置和城市名

在用戶首次進入某頁面(需要地理位置授權)時候,在頁面進行onLoad,onShow時候,進行調(diào)用wx.getLocation要求用戶進行授權;以后每次進入該頁面時,通過wx.getSetting接口,返回用戶授權具體信息。

wx.getSetting接口具體API地址鏈接為點擊打開鏈接

微信小程序怎么獲取當前位置和城市名

 上圖中scope.userLocation就是地理授權的標志;

當該標志是underfind,表示用戶初次進入該頁面,當該標志是false,表示用戶初次進入該頁面拒絕了地理授權,應進行重新要求獲取授權。

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: function (res) {
       if (res.cancel) {
        wx.showToast({
         title: '拒絕授權',
         icon: 'none',
         duration: 1000
        })
       } else if (res.confirm) {
        wx.openSetting({
         success: function (dataAu) {
          if (dataAu.authSetting["scope.userLocation"] == true) {
           wx.showToast({
            title: '授權成功',
            icon: 'success',
            duration: 1000
           })
           //再次授權,調(diào)用wx.getLocation的API
          } else {
           wx.showToast({
            title: '授權失敗',
            icon: 'none',
            duration: 1000
           })
          }
         }
        })
       }
      }
     })
    } else if (res.authSetting['scope.userLocation'] == undefined) {
     //調(diào)用wx.getLocation的API
    }
    else {
     //調(diào)用wx.getLocation的API
    }
   }
  })

在拿到用戶授權以后,使用微信的API獲取當前位置的經(jīng)緯度微信獲取位置API

微信小程序怎么獲取當前位置和城市名

這里,我們進行使用的是騰訊位置服務;專為小程序開發(fā)者提供LBS數(shù)據(jù)服務工具包,可以在小程序中調(diào)用騰訊位置服務的POI檢索、關鍵詞輸入提示、地址解析、逆地址解析、行政區(qū)劃和距離計算等數(shù)據(jù)。 

    1,得到開發(fā)者秘鑰

    2,下載微信小程序javaScriptSDK,

    3,安全域名設置,在“設置” -> “開發(fā)設置”中設置request合法域名,添加http://api.map.qq.com

在文件中引入對應的javaScriptSDK文件

var QQMapWX = require('../../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;

在文件中進行js調(diào)用

微信小程序怎么獲取當前位置和城市名

 最后的結(jié)果就是可以獲得自己所在城市的具體位置了

微信小程序怎么獲取當前位置和城市名

index.js部分的代碼

//index.js
//獲取應用實例
const app = getApp();
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
 data: {
  province: '',
  city: '',
  latitude: '',
  longitude: ''
 },
 onLoad: function () {
  qqmapsdk = new QQMapWX({
   key: 'XXXX-XXXX-XXXX-XXXX' //這里自己的key秘鑰進行填充
  });
 },
 onShow: function () {
  let vm = this;
  vm.getUserLocation();
 },
 getUserLocation: function () {
  let vm = this;
  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: function (res) {
       if (res.cancel) {
        wx.showToast({
         title: '拒絕授權',
         icon: 'none',
         duration: 1000
        })
       } else if (res.confirm) {
        wx.openSetting({
         success: function (dataAu) {
          if (dataAu.authSetting["scope.userLocation"] == true) {
           wx.showToast({
            title: '授權成功',
            icon: 'success',
            duration: 1000
           })
           //再次授權,調(diào)用wx.getLocation的API
           vm.getLocation();
          } else {
           wx.showToast({
            title: '授權失敗',
            icon: 'none',
            duration: 1000
           })
          }
         }
        })
       }
      }
     })
    } else if (res.authSetting['scope.userLocation'] == undefined) {
     //調(diào)用wx.getLocation的API
     vm.getLocation();
    }
    else {
     //調(diào)用wx.getLocation的API
     vm.getLocation();
    }
   }
  })
 },
 // 微信獲得經(jīng)緯度
 getLocation: function () {
  let vm = this;
  wx.getLocation({
   type: 'wgs84',
   success: function (res) {
    console.log(JSON.stringify(res))
    var latitude = res.latitude
    var longitude = res.longitude
    var speed = res.speed
    var accuracy = res.accuracy;
    vm.getLocal(latitude, longitude)
   },
   fail: function (res) {
    console.log('fail' + JSON.stringify(res))
   }
  })
 },
 // 獲取當前地理位置
 getLocal: function (latitude, longitude) {
  let vm = this;
  qqmapsdk.reverseGeocoder({
   location: {
    latitude: latitude,
    longitude: longitude
   },
   success: function (res) {
    console.log(JSON.stringify(res));
    let province = res.result.ad_info.province
    let city = res.result.ad_info.city
    vm.setData({
     province: province,
     city: city,
     latitude: latitude,
     longitude: longitude
    })
   },
   fail: function (res) {
    console.log(res);
   },
   complete: function (res) {
    // console.log(res);
   }
  });
 }
})

頁面展示部分的代碼



  
  所在城市
  
 

以上是“微信小程序怎么獲取當前位置和城市名”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司行業(yè)資訊頻道!

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。


網(wǎng)頁標題:微信小程序怎么獲取當前位置和城市名-創(chuàng)新互聯(lián)
當前地址:http://www.dlmjj.cn/article/eeope.html