Skip to main content

MarkerCollection

The MarkerCollection object enables you to apply bulk actions to a collection of markers. The markers can be organized by tags so that you can apply the bulk actions to a subset of the markers.

Example usage​

const markerCollection = G.markerCollection();
markerCollection.add(marker1, 'tag1');
markerCollection.add(marker2, ['tag2']);
markerCollection.add(marker3, ['tag1', 'tag2']);
markerCollection.add(marker4, 'tag2');
markerCollection.add(marker5, 'tag3');
markerCollection.add(marker6, 'tag4');
markerCollection.add(marker7, ['tag1', 'tag3', 'tag4']);
markerCollection.add(marker8);
markerCollection.add(marker9);

As you can see, you can assign one tag, multiple tags, or no tags.

You can pass a single tag as a string, a single tag as an array, or an array of tags. Tags must be a string value.

If you assign no tag then an internal default tag is applied. This is intended to allow you to use a marker collection with no tags if desired. Typically you would not mix markers with tags and those with no tags in the same collection. There is no way to target only those markers with the internal default tag.

Creating the MarkerCollection object​

G.markerCollection(): MarkerCollection

The MarkerCollection object does not take any parameters.

const markerCollection = G.markerCollection();

Properties​

PropertyTypeDescription
markersobjectThe markers in the collection, grouped by tag. Each key is a tag and each value is a Set of the Marker objects assigned to that tag. Markers added without a tag are stored under an internal default tag.

Methods​

add​

add(marker: Marker, tag?: string|string[]): void

Add a marker to the collection with zero or more tags.

ParameterTypeRequiredDescription
markerMarkersvg fileThe marker object to add.
tagstring|string[]A single tag string or an array of tag strings. If not set, the marker is assigned to an internal default tag.

No tags:

markerCollection.add(marker);

One tag:

markerCollection.add(marker, 'tag1');
markerCollection.add(marker, ['tag1']);

Multiple tags:

markerCollection.add(marker, ['tag1', 'tag2', 'anotherTag', 'andAnotherTag']);

clear​

clear(): void

Hide all the markers in the collection, then remove all markers and tags from the collection.

markerCollection.clear();

clone​

clone(): MarkerCollection

Return a new MarkerCollection with the same markers and tags. The Marker objects themselves are not copied, so both collections hold the same markers.

const copy = markerCollection.clone();

hasData​

hasData(): boolean

Return whether the collection has at least one marker.

if (markerCollection.hasData()) {
// There is at least one marker in the collection
}

hide​

hide(tag: string|string[]): void

Hide the markers that are assigned to the passed tag(s).

ParameterTypeRequiredDescription
tagstring|string[]svg fileA single tag string or an array of tag strings.

If multiple tags are passed then any marker that is assigned to one of the passed tags is hidden. Multiple tags does not mean that a marker has to be assigned to all of the passed tags.

If no tags are passed then nothing happens.

Hide markers that are assigned to the passed tag:

markerCollection.hide('tag1');
markerCollection.hide(['tag1']);

Hide markers that are assigned to the passed tags:

markerCollection.hide(['tag1', 'tag2', 'anotherTag']);

hideAll​

hideAll(): void

Hide all the markers in the collection.

markerCollection.hideAll();

isEmpty​

isEmpty(): boolean

Return whether the collection has no markers.

if (markerCollection.isEmpty()) {
// There are no markers in the collection
}

remove​

remove(marker: Marker, tag?: string|string[]): void

Remove a marker from the collection. Optionally only remove it from the passed tags.

ParameterTypeRequiredDescription
markerMarkersvg fileThe marker object to remove.
tagstring|string[]A single tag string or an array of tag strings. If not set, the marker is removed from all tags.

Removing a marker from the collection does not hide it on the map.

Remove the marker from all tags:

markerCollection.remove(marker);

Remove the marker from one tag:

markerCollection.remove(marker, 'tag1');
markerCollection.remove(marker, ['tag1']);

Remove the marker from multiple tag:

markerCollection.remove(marker, ['tag1', 'tag2', 'anotherTag', 'andAnotherTag']);

show​

show(tag: string|string[], map: Map): void

Show the markers that are assigned to the passed tag(s).

ParameterTypeRequiredDescription
tagstring|string[]svg fileA single tag string or an array of tag strings.
mapMapsvg fileThe map object

If multiple tags are passed then any marker that is assigned to one of the passed tags is shown. Multiple tags does not mean that a marker has to be assigned to all of the passed tags.

If no tags are passed then nothing happens.

Show markers that are assigned to the passed tag:

markerCollection.show('tag1', map);
markerCollection.show(['tag1'], map);

Show markers that are assigned to the passed tags:

markerCollection.show(['tag1', 'tag2', 'anotherTag'], map);

showAll​

showAll(map: Map): void

Show all the markers in the collection.

ParameterTypeRequiredDescription
mapMapsvg fileThe map object
markerCollection.showAll(map);