收藏
回答

wx.createMapContext().getScale()不能正常使用?

因为我想实现在地图上使用控件缩小放大scale,然后同时触屏两拇指向内向外滑动同样实现缩小放大scale,但是在控件函数中调用wx.createMapContext('map').getScale()不能正常运行。0.0

// 地图控件点击事件
    bindcontroltap: function(e) {
        // 判断点击的是哪个控件 e.controlId代表控件的id,在页面加载时的第3步设置的id
        var that = this;
        if (e.controlId === 2 && a < 20) {  //a的作用是计数scale的值并判断是否超出了scale的3-20的范围
            wx.createMapContext('BatteryMap').getScale({
                success: (res) => {
                    console.log("视野变化的scale:", res.scale, "原先的scale:", that.data.scale)
                    }
                    })
             
            console.log("点击+前的scale:", that.data.scale)
 
            that.setData({
                scale: that.data.scale + 1,
            })
            console.log("点击+后的scale:", that.data.scale)
                ++a;
            wx.createMapContext('BatteryMap').getCenterLocation({
                success: (res) => {
                    console.log("获取当前地图中心的经纬度", res)
                    that.setData({
                        centerLatitude: res.latitude,
                        centerLongitude: res.longitude,
                        longitude: res.longitude,
                        latitude: res.latitude,
                    })
                    console.log(that.data.centerLatitude, that.data.centerLongitude)
                    that.getbatteryInfo();
                },
            })
            //console.log("scale===" + this.data.scale) //测试打印scale是否增减
        } else if (e.controlId === 3 && a > 4) {
            console.log("点击-前的scale:", that.data.scale)
 
            that.setData({
                scale: that.data.scale - 1,
            })
 
            console.log("点击-后的scale:", that.data.scale)
 
            --a;
            wx.createMapContext('BatteryMap').getCenterLocation({
                success: (res) => {
                    console.log("获取当前地图中心的经纬度", res)
                    that.setData({
                        centerLatitude: res.latitude,
                        centerLongitude: res.longitude,
                        longitude: res.longitude,
                        latitude: res.latitude,
                    })
                    console.log(that.data.centerLatitude, that.data.centerLongitude)
                    that.getbatteryInfo();
                },
            })
            //console.log("scale===" + this.data.scale) //测试打印scale是否增减
            //  console.log("a:" + a)
        } else if (e.controlId === 1) {
            that.setData({
                mapstate: false
            })
        }
    },
 
bindregionchange: function(e) {
        var that = this;
        if(e.type == "end"){
            wx.createMapContext('BatteryMap').getScale({
                success: function(e) {
                    console.log("视野变化的scale:", e.scale, "原先的scale:", that.data.scale)
                    that.setData({
                        scalechange:e.scale,
                    })
                    console.log("scalechange的值", that.data.scalechange)
                    if (e.scale >= 15) {
                        wx.createMapContext('BatteryMap').getCenterLocation({
                            success: (res) => {
                                console.log("获取当前地图中心的经纬度", res)
                                that.setData({
                                    centerLatitude: res.latitude,
                                    centerLongitude: res.longitude,
                                })
                                console.log(that.data.centerLatitude, that.data.centerLongitude)
                                wx.request({
                                    url:app.data.ip + "/api/device/getAreaByLocation", //batteryInfo接口
                                    method: 'POST',
                                    data: {
                                        //zoneId: that.data.checkzoneId //如果没有选择工区ID发工区列表第一个工区的电池信息
                                        latitude: that.data.centerLatitude,
                                        longitude: that.data.centerLongitude,
                                    },
                                    header: {
                                        "Content-Type": "application/x-www-form-urlencoded",
                                        "Cookie": "JSESSIONID=" + app.globalData.JSESSIONID
                                    },
                                    success: (res) => {
                                        var temp = 0;
                                        var charge = 0;
                                        var j = 0
                                        console.log("获取电池信息成功:", res)
                                        //console.log(res.data.bInfoes.length)
                                        // if (res.data.result == true) {
                                        for (j; j < res.data.bInfoes.length; j++) {
                                            var item = "markers[" + j + "].";
             
                                            if (res.data.bInfoes[j].temp == undefined) {
                                                res.data.bInfoes[j].temp = "N/C"
                                            }
             
                                            if (res.data.bInfoes[j].charge == undefined) {
                                                res.data.bInfoes[j].charge = "N/C"
                                            }
             
                                            that.setData({
                                                [item + "id"]: res.data.bInfoes[j].deviceId,
                                                [item + "title"]: "设备编号:" + res.data.bInfoes[j].deviceId + "\n温度是:" + res.data.bInfoes[j].temp +
                                                    "℃\n电量是:" + res.data.bInfoes[j].charge + "V\n信息采集时间是:\n" + res.data.bInfoes[j].acqtime,
                                                [item + "latitude"]: res.data.bInfoes[j].Latitude,
                                                [item + "longitude"]: res.data.bInfoes[j].longitude,
                                                [item + "width"]: 40,
                                                [item + "height"]: 40,
                                                [item + "iconPath"]: '../../image/markers.png',
                                                // [item + "label.content"]: "温度是:" + res.data.bInfoes[j].temp + "电量是:" + res.data.bInfoes[j].charge + "\n" +
                                                //  res.data.bInfoes[j].acqtime
                                            })
                                        }
                                        //console.log(that.data.markers.length)
                                        // }
                                    },
                                    fail: function(res) {
                                        // fail
                                        wx.showModal({
                                            title: '提示',
                                            content: "调用获取设备信息接口失败",
                                            showCancel: false
                                        })
                                    },
                                    complete: function(res) {
                                        // complete
                                    }
                                })
                            }
                        })
                    } else {
                        that.setData({
                            markers: []
                        })
                    }
             
                }
             
            })
        }
         

    },




打断点打在这两行,程序根本不运行到第245行,运行到243行时直接跳到249行

最后一次编辑于  09-18
回答关注问题邀请回答
收藏

1 个回答

问题标签