title: Bing Maps Api clear a layer
body: <p>I have a page that works well it loads a Bing Map and creates a layer which is then filled with Polygons.  I then need to reload the JSON data that makes the polygons and this again works, however the data is then added to another layer so appears over the top.  I have tried to delete the layer, clear the layer etc etc but nothing seems to work.</p>
  
  <p>Any idea please.</p>
  
  <p>This is the function that does it all...</p>
  
  <pre><code>        function AddData() {
  
          dataLayer = new Microsoft.Maps.Layer();
  
          Microsoft.Maps.loadModule('Microsoft.Maps.GeoJson', function () {
              var featureCollection = Microsoft.Maps.GeoJson.read(json, {
                  polygonOptions: {
                      strokeColor: 'LightSkyBlue',
                      strokeThickness: 2
                  }
              });
  
              for (var i = 0; i &lt; featureCollection.length; i++) {
                  var fillColour = featureCollection[i].metadata.FillColor;
                  featureCollection[i].setOptions({ fillColor: fillColour });
  
                  Microsoft.Maps.Events.addHandler(featureCollection[i], 'click', displayClickBox);
                  Microsoft.Maps.Events.addHandler(featureCollection[i], 'mouseover', displayMouseOverBox);
                  Microsoft.Maps.Events.addHandler(featureCollection[i],'mouseout', displayMouseOut);
                  dataLayer.add(featureCollection[i], 0);
              }
              map.layers.insert(dataLayer);
          });
      }
  
          var getJson = function () {
          var onContentComplete = function (response) {
              //Load the JSON data into the local variable for use latter in the project...
              json = response.data;
              //load the map now that we have the polygon data...
              AddData();
          };
  
          var onError = function (reason) {
              //An error has occured so display a message to the user...
              $scope.error = "Server communication error please try again...";
              //Log the error to the console for admin debug...
              console.log(reason.data);
          };
  
          //Load the JSON for the map polygons into memory ready for display...
          $http.get("../JSON/MapData.json")
              .then(onContentComplete, onError);
      }
  </code></pre>
  
  <p>As I have said I have tried to clear the layer first using</p>
  
  <pre><code>dataLayer.clear();
  </code></pre>
  
  <p>But that seems to do nothing.</p>
  
  <p>Help please as I have been working at this for hours now.</p>
  
  <p>Thanks</p>
  
  <p>Cliff.</p>