腾讯云获取位置转为省市区
腾讯位置服务提供了多种SDK程序包,其中的JavaScript版本的SDK适用于微信小程序,所以我们下载这个SDK包。
在小程序项目中,创建lib
目录,把SDK文件放入其中
js
<script>
var QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');
var qqmapsdk;
export default {
data() {
return {
addrlocal: null,
}
},
methods: {
myLocation() { //5LFBZ-5HV6L-SAIPY-E6BNI-O7FOJ-XNFAN
const self = this; // 保存Vue实例的引用
console.log(self.addrlocal);
//获取地理定位
uni.getLocation({
type: 'wgs84',
success: function(resp) {
let latitude = resp.latitude;
let longitude = resp.longitude;
qqmapsdk.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude
},
success: function(resp) {
// console.log(resp.result)
let address = resp.result.address
let addressComponent = resp.result.address_component
let nation = addressComponent.nation;
let province = addressComponent.province;
let city = addressComponent.city;
let district = addressComponent.district;
console.log("address:" + address +
"addressComponent:" + addressComponent +
"nation:" + nation +
"province:" + province +
"city:" + city +
"district:" + district)
self.addrlocal = city + district; // 使用保存的self
console.log(self.addrlocal);
/**
* address:吉林省长春市南关区东岭南街3355号addressComponent:[object Object]nation:中国province:吉林省city:长春市district:南关区
*/
}
})
}
})
}
},
components: {
"fuwu": fuwuItem
},
onLoad: function() {
qqmapsdk = new QQMapWX({
key: '5LFBZ-5HV6L-SAIPY-E6BNI-O7FOJ-XNFAN'
});
this.myLocation();
}
}
</script>