Shiva Krishna M Shiva Krishna M - 4 months ago 98
Javascript Question

How do I clear clustered pushpins from a Bing Map in web API?



var map, clusterLayer;
function GetMap() {
map = new Microsoft.Maps.Map('#myMap',{
credentials: 'My Key Here',
zoom: 3
});
Microsoft.Maps.loadModule("Microsoft.Maps.Clustering", function () {
//Generate 3000 random pushpins in the map view.
var pins = Microsoft.Maps.TestDataGenerator.getPushpins(3000, map.getBounds());
//Create a ClusterLayer with options and add it to the map.
clusterLayer = new Microsoft.Maps.ClusterLayer(pins, {
clusteredPinCallback: createCustomClusteredPin,
gridSize: 80
});
map.layers.insert(clusterLayer);
});

}

function removeEverything() {
// None of the following functions seem to work
//map.entities.clear() ;
//map.layers.clear() ;
map.layers.remove(clusterLayer) ;
}

function createCustomClusteredPin(){
// Some Decoration
}


<html>

<head>

<title></title>

<meta charset="utf-8" />

<script type='text/javascript'

src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>
</head>

<body>

<div id="myMap" style="position:relative;width:600px;height:400px;"></div>

</body>

</html>





I have a clustered layer of pushpins on a Bing Map which I generated using the JavaScript API. I created a clustered layer of pushpins and added it to the map. The problem is that I am unable to remove the pushpins from the map once I have added it. I tried many functions from the API but nothing seems to work. The above is my code. Ideally I would also want a feature to hide the clusterLayer from the map and present it on a button click. For this I used the clusterLayer.setOptions({visible:false}) but that is not working too. What am I missing ? Any help is much appreciated.

PS : I am calling the removeEverything() function from the console.

Answer

Functionality to clear a clustering layer was added last week to the experimental branch. Add "&branch=experimental" to the map script URL. Then you can clear the cluster layer by doing this:

clusterLayer.clear();
Comments