uniapp小程序實現定位


https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.onLocationChange.html

 

 

 

// #ifdef MP-WEIXIN
//
定位方法 getUserLocation: function() { var _this = this; wx.getSetting({ success: (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 }) _this.locationRole = false; } else if (res.confirm) { //確定授權,通過wx.openSetting發起授權請求 wx.openSetting({ success: function(res) { if (res.authSetting[ "scope.userLocation"] == true) { wx.showToast({ title: '授權成功', icon: 'success', duration: 1000 }) _this.locationRole = true; //再次授權,調用wx.getLocation的API _this.geo(); } else { wx.showToast({ title: '授權失敗', icon: 'none', duration: 1000 }) _this.locationRole = false; } } }) } } }) } else if (res.authSetting['scope.userLocation'] == undefined) { //用戶首次進入頁面,調用wx.getLocation的API _this.geo(); } else { console.log('授權成功') _this.locationRole = true; //調用wx.getLocation的API _this.geo(); } } }) },
// 獲取定位城市
            geo: function() {
                var _this = this;
                wx.showLoading({
                    title: '定位中...',
                    mask: true,
                })
                wx.getLocation({
                    type: 'gcj02',
                    success: function(res) {
                        console.log('location111', res)
                        _this.latitude = res.latitude
                        _this.longitude = res.longitude
                        wx.hideLoading()
                        _this.stationDetails()
                    }
                });
                new Promise((resolve, reject) => {
                    let _locationChangeFn = (res) => {
                        console.log('location change', res)
                        _this.latitude = res.latitude
                        _this.longitude = res.longitude
                        _this.stationDetails()
                        wx.hideLoading()
                        // wx.offLocationChange(_locationChangeFn)
                    }
                    wx.startLocationUpdate({
                        success: (res) => {
                            console.log(res, 5656);
                            wx.onLocationChange(_locationChangeFn)
                            resolve()
                        },
                        fail: (err) => {
                            console.log('獲取當前位置失敗', err)
                            wx.hideLoading()
                            reject()
                        }
                    })
                })

                
            },
            // #endif

 


免責聲明!

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



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