3.7 Metro Web: Removing routes

Removing routes

You will notice, however, that every time you click on a new route, more and more icons keep piling up on your map. Soon, your entire maproom is covered with icons, and everything has gotten very sluggish. In order to avoid this, we need to remove any previous route before we add a new route. This ensures that only the route that the user has clicked on is visible at any given time.

The way that google maps allows you to turn things on and off, is by using the “setMap” function. To turn something on, and I say “something” because “something” can be a placemarker, line, polygon, circle, or kml layer, you use the following:

And, to turn that something off:

In the case of our bus stop placemarkers, we have put each marker in the busstop array. This means that in order to erase all the markers, you would have to do the following:

But this is highly inefficient, and besides, each route that a user clicks on has a different number of bus stop locations. So, in order to delete all the markers for a given route, we need to simply loop through every busstop marker in the array. We will do so by creating a new function.

Let’s break this function down.

  • “if(busstop)” checks to see if there are any values in the busstop array. If so, it executes the following lines of code within the { }
  • “for (i in busstop)” loops through every value in the busstop array
  • “busstop[i].setMap(null);” deletes the placemarker for that value in the array
  • “busstop = []” resets the busstop array

Now you just need to call this function before you map a new route. Add the call to removeLayer in the “mapBusStops” function.

(sample code)

Leave a Reply