需要用到微信小程序JavaScript SDK:
// 获取当前定位的经纬度
getLocation() {
debugger
let that = this
wx.getSetting({
success: (res) => {
if (!res.authSetting['scope.userLocation']) {
wx.getLocation({
type: 'wgs84',
success: function (res) {
qqmapsdk = new QQMapWX({
key: '…………' //这里用自己的key秘钥进行填充
});
that.getLocal(res.latitude, res.longitude)
},
fail() { // 拒绝获取地理位置
wx.showModal({
content: '您暂未开启地理位置权限,是否开启',
confirmText: '去开启',
cancelText: '取消',
success(res) {
if (res.confirm) {
wx.openSetting({
scope: 'scope.userLocation',
success(res) {
wx.getLocation({
type: 'wgs84',
success: function (res) {
qqmapsdk = new QQMapWX({
key: '…………' //这里用自己的key秘钥进行填充
});
that.getLocal(res.latitude, res.longitude)
},
})
},
fail() {
wx.showToast({
title: res,
icon: 'none',
duration: 2000
})
}
})
} else if (res.cancel) {
wx.showToast({
title: '你已拒绝获取地理位置信息',
icon: 'none',
duration: 2000
})
that.setData({
showTab: false
})
}
}
})
}
})
} else {
wx.getLocation({
type: 'wgs84',
success: function (res) {
qqmapsdk = new QQMapWX({
key: '…………' //这里用自己的key秘钥进行填充
});
that.getLocal(res.latitude, res.longitude)
},
})
}
},
})
},
// 获取当前城市名字
getLocal: function (latitude, longitude) {
let that = this;
//位置坐标
qqmapsdk.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude
},
success: function (res) {
that.setData({
locationCity: res.result.address_component.city.split('市')[0],
……
})
that.getProjectList(res.result.address_component.city.split('市')[0])
},
fail: function (res) {
that.setData({
locationCity: '获取地理位置失败'
})
}
});
}