Event handling in objects of ObjectManager
ObjectManager - A class for adding a large number of objects to the map without needing to create placemarks individually.
The objects added to the ObjectManager are located in the ObjectManager.objects collection. Clusters formed from the added placemarks are added to the ObjectManager.clusters collection. The visual representation of placemarks is created asynchronously on demand. The visual representation of placemarks and clusters are objects implementing the IOverlay interface. In particular, overlay.Placemark. Overlays are placed in the ObjectManager.objects.overlays and ObjectManager.clusters.overlays collections, respectively.
Events on object overlays propagate to the parent collections. The unique ID of the object on which the event occurred is passed in the objectID field of the event. IDs for single objects are set by the user, and for clusters they are generated automatically during cluster creation.
<!DOCTYPE html>
<html>
<head>
<title>Examples. Event handling in ObjectManager</title>
<meta
http-equiv="Content-Type"
content="text/html; charset=UTF-8"
/>
<!--
Set your own API-key. Testing key is not valid for other web-sites and services.
Get your API-key on the Developer Dashboard: https://developer.tech.yandex.ru/keys/
-->
<script
src="https://api-maps.yandex.ru/2.1/?lang=en_RU&apikey=<your API-key>"
type="text/javascript"
></script>
<script
src="https://yandex.st/jquery/2.2.3/jquery.min.js"
type="text/javascript"
></script>
<script
src="object_manager_events.js"
type="text/javascript"
></script>
<style>
html,
body,
#map {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<div id="map"></div>
</body>
</html>
ymaps.ready(init);
function init() {
var myMap = new ymaps.Map(
"map",
{
center: [55.76, 37.64],
zoom: 10,
},
{
searchControlProvider: "yandex#search",
}
),
objectManager = new ymaps.ObjectManager({
// Setting an option to make placemarks start clusterizing.
clusterize: true,
geoObjectOpenBalloonOnClick: false,
clusterOpenBalloonOnClick: false,
});
myMap.geoObjects.add(objectManager);
$.ajax({
url: "data.json",
}).done(function (data) {
objectManager.add(data);
});
function onObjectEvent(e) {
var objectId = e.get("objectId");
if (e.get("type") == "mouseenter") {
// The setObjectOptions method allows you to set object options "on the fly".
objectManager.objects.setObjectOptions(objectId, {
preset: "islands#yellowIcon",
});
} else {
objectManager.objects.setObjectOptions(objectId, {
preset: "islands#blueIcon",
});
}
}
function onClusterEvent(e) {
var objectId = e.get("objectId");
if (e.get("type") == "mouseenter") {
objectManager.clusters.setClusterOptions(objectId, {
preset: "islands#yellowClusterIcons",
});
} else {
objectManager.clusters.setClusterOptions(objectId, {
preset: "islands#blueClusterIcons",
});
}
}
objectManager.objects.events.add(
["mouseenter", "mouseleave"],
onObjectEvent
);
objectManager.clusters.events.add(
["mouseenter", "mouseleave"],
onClusterEvent
);
}