错误写法
public.js:
var QQMapWX = require('qqmap-wx-jssdk.js'); //引入官方插件
var qqmapsdk = new QQMapWX({ key: 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX-XXXXX' });
function getLocation() {
var data;
wx.getLocation({
type: 'wgs84',
success: function (res) {
let latitude = res.latitude;
let longitude = res.longitude;
qqmapsdk.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude
},
success: function (res) {
data = res.result;
}
});
},
});
return data;
}
index.js:
var publicJS = require('../utils/public.js');
locationSelect: function() {
var locationData = publicJS.getLocation();
console.log(locationData);
}
更改
把回调函数的值用 promise resolve出去 用async+await接受 或者 .then接受
public.js
function getLocation() {
return new Promise(function(resolve, reject){
wx.getLocation({
type: 'wgs84',
success: function (res) {
let latitude = res.latitude;
let longitude = res.longitude;
qqmapsdk.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude
},
success: function (res) {
resolve(res.result);
}
});
},
});
})
}
index:js
var publicJS = require('../utils/public.js');
locationSelect: function() {
publicJS.getLocation().then(function(locationData){
console.log(locationData);
})
}