Google Maps API v3 remove all polylines

前端 未结 2 802
青春惊慌失措
青春惊慌失措 2021-02-04 02:17

Little background. I have a navigation setup for when you click on a certain navigation item, it creates markers on the map. If you click on a different navigation

相关标签:
2条回答
  • 2021-02-04 02:30

    You have to do polyline.setMap(null), that will remove the line from the map. Documentation.

    0 讨论(0)
  • 2021-02-04 02:34

    polyline is just an array of LatLng objects, not individual Polylines. I think you probably need a separate array for the polylines, which you can then loop over to remove them all. Create a global array line.

     var line = [];
     polyline = new google.maps.Polyline({
            path: points,
            strokeColor: "#FF0000",
            strokeOpacity: 1.0,
            strokeWeight: 2
        });
     line.push(polyline);
    

    Now you are pushing all the polyline objects into an array line. You can make it invisible or remove it from the map by looping it like this:

    for (i=0; i<line.length; i++) 
    {                           
      line[i].setMap(null); //or line[i].setVisible(false);
    }
    
    0 讨论(0)
提交回复
热议问题