JavaScript/jQuery Map - Handle the Related Events

The Map UI component fires the markerAdded and markerRemoved events when a marker is added or removed. To handle them, assign functions to the onMarkerAdded and onMarkerRemoved properties, respectively.

JavaScript
  • $(function() {
  • $("#mapContainer").dxMap({
  • center: { lat: 40.749825, lng: -73.987963 },
  • zoom: 10,
  • onMarkerAdded: function (e) {
  • const addedMarker = e.options;
  • // The original marker used by the current map provider
  • const originalMarker = e.originalMarker;
  • // Event handling commands go here
  • },
  • onMarkerRemoved: function (e) {
  • const removedMarker = e.options;
  • // Event handling commands go here
  • }
  • });
  • });

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the markerAdded and markerRemoved events using the on(eventName, eventHandler) method.

JavaScript
  • const markerRemovedHandler1 = function (e) {
  • const removedMarker = e.options;
  • // First handler of the "markerRemoved" event
  • };
  •  
  • const markerRemovedHandler2 = function (e) {
  • const removedMarker = e.options;
  • // Second handler of the "markerRemoved" event
  • };
  •  
  • $("#mapContainer").dxMap("instance")
  • .on("markerRemoved", markerRemovedHandler1)
  • .on("markerRemoved", markerRemovedHandler2);
See Also