第一步:设置app.json:
"permission": { "scope.userLocation": { "desc": "用于提供位置信息水印拍摄" } }, "requiredPrivateInfos": [ "getLocation"//这个需要在小程序后台:开发-开发管理-接口设置 中开通 ]
第二步:去腾讯地图中申请自己的key
记得要勾选WebServiceApi
第三步:下载sdk文件(选择你需要的版本并放到你的项目文件中)
第四步: 在页面中进行配置
①
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmap;
②
data: {
myLatitude: "",
myLongitude: "",
myAddress: "",
}
③
onLoad() {
qqmap = new QQMapWX({
key:app.globalData.keyVal //你自己的key
});
var that = this
wx.getLocation({
type:"gcj02",
altitude:true,
isHighAccuracy:true,
highAccuracyExpireTime:2000,
success(res){
console.log('ddddd',res)
that.setData({myLatitude: res.latitude, myLongitude: res.longitude})
//用腾讯地图的api,根据经纬度获取城市
qqmap.reverseGeocoder({
location: {
latitude: that.data.myLatitude,
longitude: that.data.myLongitude
},
get_poi: 1,
poi_options: 'policy=1;radius=1000;page_size=30;page_index=1',
success: function (res) {
console.log(res)
var a = res.result.address_component
//获取市和区(区可能为空)
that.setData({myAddress: a.city + a.district})
//控制台//输出结果
console.log(that.data.myAddress)
},
fail:function(res) {
console.log(res);
},
complete: function(res) {
//console.log(res);
}
// console.log('获取位置:',res)
})
}
})
}
结果:(#^.^#)
当然这个是在开发工具上的结果,精准结果还是得用手机看