UnitSelection

A collection of zero or more Unit instances. A UnitSelection is returned by UnitMap.units() and provides a chainable API for querying and manipulating units.

map.on("ready", function () {
  // Get a unit selection instance from the map:
  var units = map.units();

  // Color all units:
  units.color("#ff00ff");

  // Zoom and pan the map to fit every unit in the collection:
  map.fitBounds(units.bounds(), { padding: 100 });
});

UnitSelection.where([tags])

Get the Unit instances in the collection which match the given tags.

Arguments

ArgumentRequiredTypeDefault ValueDescription
tagsNoObject{}An object of key-value pairs to filter by, where each key is the name of a tag and each value is the value of the tag. Defaults to an empty object, which returns all Units.

Returns

A new UnitSelection instance.

UnitSelection.find(id)

Get all Unit instances in the collection which match the given id or ids.

Arguments

ArgumentRequiredTypeDefault ValueDescription
idYesString | ArrayundefinedA single id or an array of ids to look for.

Returns

A new UnitSelection instance.

UnitSelection.first()

Get the first Unit in the collection.

Returns

A new UnitSelection instance.

UnitSelection.at(index)

Get the Unit at the given index in the collection.

Arguments

ArgumentRequiredTypeDefault ValueDescription
indexYesIntegerundefinedThe index of the desired Unit.

Returns

A new UnitSelection instance.

UnitSelection.color(color)

Set the fill color for each Unit instance in the collection. This method is chainable.

Arguments

ArgumentRequiredTypeDefault ValueDescription
colorYesStringundefinedA valid CSS color.

Returns

The object chain.

UnitSelection.center()

Get each Unit instance's center in the UnitSelection.

Returns

An Array of [x, y] coordinates Arrays.

UnitSelection.bounds()

Get the combined bounding box that spans every Unit instance in the collection, in map space.

// Zoom and pan the map to fit a specific set of units.

map.fitBounds(map.units().find(["1", "2", "3"]).bounds(), { padding: 100 });

Returns

A bounding box as a nested Array of the form [[minX, minY], [maxX, maxY]], suitable for passing to UnitMap.fitBounds. Returns null when the collection is empty or none of its Units have an area.

UnitSelection.data()

Get the UnitSelection data as JSON.

Returns

A JSON object.


What’s Next