Leaflet .locate 手表选项在更改选项卡 Ionic 3 后中断 .locate

Leaflet .locate watch option breaks .locate after changing tab Ionic 3

我有一个名为 loadmap(){} 的函数,我正在创建 map.Im 使用

加载此函数
 ionViewDidEnter() {
this.loadmap();


  }

我有内部负载图

  this.map = leaflet.map("map").fitWorld();

这就是我初始化地图的方式

这是我在用户更改选项卡时删除地图的方式。

ionViewDidLeave(){

    this.map.remove();


  }

这是我的 .locate 函数:

var usermarker;


  this.map.locate({
    setView: true,
    maxZoom: 120,
    watch:true,
    enableHighAccuracy:true



  }).on("locationfound", e => {
    if (!usermarker) {
      usermarker = new L.marker(e.latlng).addTo(this.map);
  } else {
      usermarker.setLatLng(e.latlng);
  }
}).on("locationerror", error => {
  if (usermarker) {
      this.map.removeLayer(usermarker);
      usermarker = undefined;
  }
});

问题出在第一次 .locate 函数 works.but 如果我更改选项卡并返回地图选项卡 .locate 函数没有 work.if 我删除监视选项它工作。

谢谢

除了map.remove()之外,您还必须调用map.stopLocate():

Stops watching location previously initiated by map.locate({watch: true})

现场演示:https://plnkr.co/edit/PKMPjfX3zD3QdWmEI0iX?p=preview(使用 "Toggle map" 按钮模拟您更改标签页)

也就是说,Leaflet 在使用 remove map method. => Merged in PR Leaflet/Leaflet#5893

时确实可以自动执行此操作