版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31653405/article/details/85783179
效果如图:
语法分割线-----------------------------
代码分割线-----------------------------
1.首先 引入 官方获取地图的js
安全域名设置,需要在微信公众平台添加域名地址https://apis.map.qq.com
2.在js文件中引入js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
3.编写wxml显示个布局:
<!--获取当前地理位置-->
<view class="weizhi_view">
<view class="weizhi_bottom" bindtap="bindKeyInput">
<image class="weizhi_img" src="../images/cal/diliweizhi.png" ></image>
<text class='weizhi_info' maxlength='100' >{{province}}{{city}}{{district}}{{street_number}}</text>
</view>
</view>
4.给wxml添加相应参数,及自动获取的函数方法:
const app = getApp();
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
data: {
province: '',
city: '',
district:'',
street_number:'',
latitude: '',
longitude: ''
},
onLoad: function () {
qqmapsdk = new QQMapWX({
key: 'DQLBZ-NXWCU-B55VJ-BZF2U-NBSRV-UAFVG' //这里自己的key秘钥进行填充
});
},
onShow: function () {
let vm = this;
vm.getUserLocation();
},
getUserLocation: function () {
let vm = this;
wx.getSetting({
success: (res) => {
console.log(JSON.stringify(res))
// res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
// res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
// res.authSetting['scope.userLocation'] == true 表示 地理位置授权
if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
wx.showModal({
title: '请求授权当前位置',
content: '需要获取您的地理位置,请确认授权',
success: function (res) {
if (res.cancel) {
wx.showToast({
title: '拒绝授权',
icon: 'none',
duration: 1000
})
} else if (res.confirm) {
wx.openSetting({
success: function (dataAu) {
if (dataAu.authSetting["scope.userLocation"] == true) {
wx.showToast({
title: '授权成功',
icon: 'success',
duration: 1000
})
//再次授权,调用wx.getLocation的API
vm.getLocation();
} else {
wx.showToast({
title: '授权失败',
icon: 'none',
duration: 1000
})
}
}
})
}
}
})
} else if (res.authSetting['scope.userLocation'] == undefined) {
//调用wx.getLocation的API
vm.getLocation();
}
else {
//调用wx.getLocation的API
vm.getLocation();
}
}
})
},
// 微信获得经纬度
getLocation: function () {
let vm = this;
wx.getLocation({
type: 'wgs84',
success: function (res) {
console.log(JSON.stringify(res))
var latitude = res.latitude
var longitude = res.longitude
var speed = res.speed
var accuracy = res.accuracy + 1000;
vm.getLocal(latitude, longitude)//根据经纬度进行获取当前地理位置信息
},
fail: function (res) {
console.log('fail' + JSON.stringify(res))
}
})
},
// 获取当前地理位置
getLocal: function (latitude, longitude) {
let vm = this;
qqmapsdk.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude
},
success: function (res) {
console.log(JSON.stringify(res));
let province = res.result.ad_info.province
let city = res.result.address_component.city
let district = res.result.address_component.district
let street_number = res.result.address_component.street_number
vm.setData({
province: province,
city: city,
district: district,
street_number: street_number,
latitude: latitude,
longitude: longitude
})
},
fail: function (res) {
console.log(res);
},
complete: function (res) {
// console.log(res);
}
});
},
bindKeyInput: function () {
let vm = this;
wx.showToast({
title: '授权成功',
icon: 'success',
duration: 1000
})
vm.getLocation();
}
})
----------------------------------------------------------------------------------------------------------------
转载声明:本文为博主原创文章,未经博主允许不得转载。
----------------------------------------------------------------------------------------------------------------
文章中,有问题,可以在评论区评论,一起探讨编程中奥秘!
----------------------------------------------------------------------------------------------------------------
来都来了,那就点个赞吧