上一篇是绘制路线,这一篇是绘制大头针获取经纬度,以及左下角还原本地位置
代码片
wxml文件
**
js文件
**
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
// 实例化API核心类
var demo= new QQMapWX({
key: '你的key' // 必填
});
Page({
data: {
controls:[],
province: ‘’,//省
city: ‘’,//市
district: ‘’,//区
}
//经纬度转换为地址:
getLocal: function (latitude, longitude) {
let vm = this;
console.log("经纬度转换为地址:")
demo.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude
},
success: function (res) {
console.log("打印1")
console.log(JSON.stringify(res));
let province = res.result.ad_info.province
let city = res.result.ad_info.city
let district = res.result.ad_info.district
console.log("目前省市区为:" + province + ":" + city + ":" + district)
console.log(res.result.location.lat)
console.log(res.result.location.lng)
vm.setData({
province: province,//省
city: city,//市
district: district,//区
})
},
//失败回调
fail: function (res) {
console.log(res);
},
//完成回调
complete: function (res) {
// console.log(res);
}
});
},
bindregionchange: function (e) {
var that = this
if (e.type == "begin") {
console.log("begin");
} else if (e.type == "end") {
var mapCtx = wx.createMapContext("map")
mapCtx.getCenterLocation({
success: function (res) {
console.log(res)
var latitude = res.latitude
var longitude = res.longitude
that.getLocal(latitude, longitude)
}
})
}
},
// 控件的点击事件
bindcontroltap: function (e) {
switch (e.controlId) {
case 1:
console.log("触发了大头针")
// 当点击图标location.png的图标,则触发事件movetoPositioon()
break;
case 2:
console.log("触发了定位")
this.movetoPosition();
break;
}
},
//移动定点
movetoPosition: function () {
var mapCtx = wx.createMapContext("map")
mapCtx.moveToLocation();
},