Remove rendered direction routes on google map

前端 未结 2 601
我寻月下人不归
我寻月下人不归 2021-01-27 10:24

I try to render direction route on google map and my issue was, when I am try to get another direction route previously rendered route not clear. I want to know how I reset rend

相关标签:
2条回答
  • 2021-01-27 10:50

    I agree with Dr.Molle .

    Still, One thing that can be useful for many overlays (like markers, like infowindows, ...): Store the objects in an array; if needed, keep that array on the global scope; then you can easily remove them from the map.

    var renderObjects = [];
    
    function clearRenderObjects() {
      for(var i in renderObjects) {
        renderObjects[i].setMap(null);
      }
    }
    
    $('#direction-form').submit(function () {
      // clear previous
      clearRenderObjects();
      ...
      var directionsRenderer = new google.maps.DirectionsRenderer();
      directionsRenderer.setMap(map);
      // add to the array
      renderObjects.push(directionsRenderer);
      ...
    });
    
    0 讨论(0)
  • 2021-01-27 10:50

    Use the same DirectionsRenderer-instance for all requests(currently you create a new instance on each request)

    0 讨论(0)
提交回复
热议问题