JS
// 引入SDK核心类,js文件根据自己业务,位置可自行放置
var QQMapWX = require('../../static/js/qqmap-wx-jssdk.js');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
key: '你的腾讯地图服务key'
});
page({
data:{},
onload:function(){},
onshow:function(e){
var t = this, a = wx.getSystemInfoSync(), e = i.getCache("sysset");
wx.getSetting({
success: (res) => {
var statu = res.authSetting;
console.log(statu,3344);
if (statu['scope.userLocation']) {
t.getLocation2();
}else{
wx.authorize({
scope: 'scope.userLocation',
success() {
t.getLocation2();
},
});
}
}
});
}
getLocation2:function(){
let t =this;
var latitude = '';
var longitude = '';
wx.getLocation({
type: 'wgs84',
success (res) {
latitude = res.latitude;
longitude = res.longitude;
t.setData({
latitude:latitude,
longitude:longitude
});
wx.setStorageSync('latitude', latitude);
wx.setStorageSync('longitude', longitude);
}
});
setTimeout(function(){
var lat = wx.getStorageSync('latitude');
var lng = wx.getStorageSync('longitude');
// 调用接口
qqmapsdk.reverseGeocoder({
sig:'你的腾讯地图服务key',
location: { latitude: lat, longitude: lng },
success: function (res) {
console.log(res,1111);
// console.log(res.result.address_component.city);
t.setData({
city:res.result.address_component.city
});
wx.setStorageSync('city', res.result.address_component.city);
},
fail:function(res){
console.log(res,555);
}
});
},1000);
})
})
小程序+腾讯地图拿到用户当前定位经纬度,获取当前城市
最新推荐文章于 2024-09-16 00:54:26 发布