m0_74195216 2024-03-17 13:30 采纳率: 62.5%
浏览 5

微信小程序导航功能,选取起点终点地址设置的一样的代码,选取结果会通用

任意选了起点或者终点的地址后,两个会同步

img


源码终点地址是确定的,我将终点地址也和起点地址一样改为了可以选择的,但是代码用的一样的,就出现了两者共用地址的情况,应该是有个变量没设置好,但是初学者不懂怎么改,希望大家帮忙看看怎么修改这个地址选取的代码才能让两者不同步而是分开选
代码图片如下

img

  • 写回答

3条回答 默认 最新

  • 檀越@新空间 2024-03-17 13:34
    关注

    m0_74195216 下午好🌅🌅🌅
    本答案参考ChatGPT-3.5

    问题是希望在微信小程序导航功能中,选取起点和终点地址设置时可以分别选取不同的地址,而不是同步选取相同的地址。根据提供的代码,需要修改选择地址的逻辑。

    解决方案如下:

    1. 找到选择起点和终点地址的相关代码。
    2. 确保起点和终点地址的选择逻辑分开,不相互依赖。
    3. 修改起点地址选择逻辑,使其能够单独选取地址,并将选取结果保存在一个变量中。
    4. 修改终点地址选择逻辑,使其能够单独选取地址,并将选取结果保存在另一个变量中。
    5. 在导航功能中使用起点和终点地址的对应变量,并分别设置到导航功能中。

    以下是修改后的代码示例:

    Page({
      data: {
        startAddress: '', // 保存起点地址的变量
        endAddress: '', // 保存终点地址的变量
      },
    
      _pickStartAddress:function(e) {
        var that this;
        console.log("选取起点地址,e");
        let latitude that.data.location.latitude;
        let longitude that.data.location.longitude;
        console.log("latitude",latitude);
        console.log("longitude",longitude);
        this.getuserLocation();
    
        // 根据选择的起点地址设置起点地址变量
        this.setData({
          startAddress: e.detail.address, // 这里根据实际的地址数据设置
        });
      },
    
      _pickEndAddress:function(e) {
        var that this;
        console.log("选取终点地址,e");
        let latitude that.data.location.latitude;
        let longitude that.data.location.longitude;
        console.log("latitude",latitude);
        console.log("longitude",longitude);
        this.getuserLocation();
    
        // 根据选择的终点地址设置终点地址变量
        this.setData({
          endAddress: e.detail.address, // 这里根据实际的地址数据设置
        });
      },
    
      navigate:function() {
        // 使用起点和终点地址变量进行导航功能
        const startAddress = this.data.startAddress;
        const endAddress = this.data.endAddress;
        // 进行导航功能的具体实现
        // ...
      }
    });
    

    通过以上修改,起点和终点地址的选择逻辑被分开,每个地址都可以单独选取,并且保存在对应的变量中。在导航功能中,可以使用起点和终点地址的变量来进行导航操作。这样就能够使起点和终点地址可以分别选取不同的地址而不是同步选取相同的地址。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月17日