EntityCollection Class
Contains a collection of entities. An Entity can be any one of the following types: Infobox, Polygon, Polyline, Pushpin, TileLayer, or EntityCollection.
Constructor
| Name | Definition | Description |
|---|---|---|
|
EntityCollection |
|
Initializes a new instance of the EntityCollection class. |
Methods
| Name | Definition | Return Value | Description |
|---|---|---|---|
|
clear |
|
None |
Removes all entities from the collection. |
|
get |
|
Entity* |
Returns the entity at the specified index in the collection. |
|
getLength |
|
number |
Returns the number of entities in the collection. |
|
getVisible |
|
boolean |
Returns whether the entity collection is visible on the map. |
|
getZIndex |
|
number |
Gets the z-index of the entity collection with respect to other items on the map. |
|
indexOf |
|
number |
Returns the index of the specified entity in the collection. If the entity is not found in the collection, -1 is returned. |
|
insert |
|
None |
Inserts the specified entity into the collection at the given index. |
|
pop |
|
Entity* |
Removes the last entity from the collection and returns it. |
|
push |
|
None |
Adds the specified entity to the last position in the collection. |
|
remove |
|
Entity* |
Removes the specified entity from the collection and returns it. |
|
removeAt |
|
Entity* |
Removes the entity at the specified index from the collection and returns it. |
|
setOptions |
|
None |
Sets the options for the entity collection. |
|
toString |
|
string |
Converts the EntityCollection object to a string. |
* An Entity can be any one of the following types: Infobox, Polygon, Polyline, Pushpin, TileLayer, or EntityCollection.
Events
| Name | Arguments | Description |
|---|---|---|
|
entityadded |
object: {collection: EntityCollection, entity:Entity*} |
Occurs when one of the following happens:
For example, if collection #1 contains an entity, which is another collection #2, then when an entity is added to collection #2, two entityadded events are fired. |
|
entitychanged |
object: {collection: EntityCollection, entity:Entity*} |
Occurs when one of the following happens:
For example, if collection #1 contains an entity, which is another collection #2, then when an entity of collection #2 changes, two entitychanged events are fired. |
|
entityremoved |
object: {collection: EntityCollection, entity:Entity*} |
Occurs when one of the following happens:
For example, if collection #1 contains an entity, which is another collection #2, then when an entity of collection #2 is removed, two entityremoved events are fired. |
* An Entity can be any one of the following types: Infobox, Polygon, Polyline, Pushpin, TileLayer, or EntityCollection.
Example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript">
var map = null;
var pinTotal = 0;
function GetMap()
{
// Initialize the map
map = new Microsoft.Maps.Map(document.getElementById("mapDiv"),
{credentials:"Bing Maps Key"});
// Add handler for the map click event - add a pin to the click location.
Microsoft.Maps.Events.addHandler(map, 'click', addPin);
}
function addPin(e) {
if (e.targetType == "map") {
// Return the location where the map was clicked and create the pin.
var point = new Microsoft.Maps.Point(e.getX(), e.getY());
var loc = e.target.tryPixelToLocation(point);
var pin = new Microsoft.Maps.Pushpin(loc);
// Attach a handler to the pin so that it is removed when it is clicked
Microsoft.Maps.Events.addHandler(pin, 'click', removePin);
// Add the pushpin
map.entities.push(pin);
}
}
function removePin(e)
{
var indexOfPinToRemove = map.entities.indexOf(e.target);
map.entities.removeAt(indexOfPinToRemove);
}
</script>
</head>
<body onload="GetMap();">
<div id='mapDiv' style="position:relative; width:400px; height:400px;"></div>
<b>Click the map to add a pin, or click a pin to remove it.<b/>
</body>
</html>