微信小程序(小程序定位獲取地址信息篇)


程序思路:

1、小程序獲取用戶定位信息(經緯度)

2、引入騰訊地圖SDK ,將經緯度傳入逆向定位方法  

3、SDK下載地址 

 

以下是代碼

 

js

// 引入SDK核心類
var QQMapWX = require('xxx/qqmap-wx.js');
 
// 實例化API核心類
var qqmapsdk = new QQMapWX({
    key: '開發密鑰(key)' // 必填
});  
 
//在Page({})中使用下列代碼
//觸發表單提交事件,調用接口
    // 獲取定位
    getLocation(){
        let that = this
        wx.getLocation({
            success: function (res) {
          //格式化經緯度
                that.formSubmit(res.latitude+ ',' + res.longitude)
            },
            fail: function(error) {
                wx.showToast({
                    title: "獲取定位失敗",
                    icon: 'none',
                    duration: 1500
                  })
            },
        })
    },

formSubmit(e) { var _this = this; qqmapsdk.reverseGeocoder({ //位置坐標,默認獲取當前位置,非必須參數 /** * //Object格式 location: { latitude: 39.984060, longitude: 116.307520 }, */ /** * //String格式 location: '39.984060,116.307520', */ location: e.detail.value.reverseGeo || '', //獲取表單傳入的位置坐標,不填默認當前位置,示例為string格式 //get_poi: 1, //是否返回周邊POI列表:1.返回;0不返回(默認),非必須參數 success: function(res) {//成功后的回調 console.log(res); var res = res.result; var mks = []; /** * 當get_poi為1時,檢索當前位置或者location周邊poi數據並在地圖顯示,可根據需求是否使用 * for (var i = 0; i < result.pois.length; i++) { mks.push({ // 獲取返回結果,放到mks數組中 title: result.pois[i].title, id: result.pois[i].id, latitude: result.pois[i].location.lat, longitude: result.pois[i].location.lng, iconPath: './resources/placeholder.png', //圖標路徑 width: 20, height: 20 }) } * **/ //當get_poi為0時或者為不填默認值時,檢索目標位置,按需使用 mks.push({ // 獲取返回結果,放到mks數組中 title: res.address, id: 0, latitude: res.location.lat, longitude: res.location.lng, iconPath: './resources/placeholder.png',//圖標路徑 width: 20, height: 20, callout: { //在markers上展示地址名稱,根據需求是否需要 content: res.address, color: '#000', display: 'ALWAYS' } }); _this.setData({ //設置markers屬性和地圖位置poi,將結果在地圖展示 markers: mks, poi: { latitude: res.location.lat, longitude: res.location.lng } }); }, fail: function(error) { console.error(error); }, complete: function(res) { console.log(res); } }) }

 

 不懂的同學歡迎留言

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM