   addMarkerLayer: (options) ->
     layer = new MarkerLayer(this, String(@nextMarkerLayerId++), options)
     @markerLayers[layer.id] = layer
+    if layer.role
+      @markerLayerIdsByRole[layer.role] ?= new Set()
+      @markerLayerIdsByRole[layer.role].add(layer.id)
     layer
 
   # Public: Get a {MarkerLayer} by id.

  createMarkerSnapshot: (activeMarkerLayer) ->
    snapshot = {}

    role = activeMarkerLayer?.getRole()

    for markerLayerId, markerLayer of @markerLayers when markerLayer.maintainHistory
      if role? and (markerLayer.getRole() is role) and (markerLayer isnt activeMarkerLayer)

    snapshot

  restoreFromMarkerSnapshot: (snapshot, activeMarkerLayer) ->
    if activeMarkerLayer?
      activeMarkerLayer = @getMarkerLayer(activeMarkerLayer.id)
      role = activeMarkerLayer.getRole()

      # When snapshot includes multiple layerSnapshot of `role`, disable snapshot restore target redirection
      # to avoid multiple layerSnapshots being restored to single markerLayer.
      layerSnapshotIdsOfRole = Object.keys(snapshot).filter (layerId) => @markerLayerIdsByRole[role].has(layerId)
      if layerSnapshotIdsOfRole.length is 1
        layerSnapshotRestoreToActiveMarkerLayer = snapshot[layerSnapshotIdsOfRole[0]]

    for markerLayerId, layerSnapshot of snapshot
      if layerSnapshotRestoreToActiveMarkerLayer? and layerSnapshotRestoreToActiveMarkerLayer is layerSnapshot
        activeMarkerLayer.restoreFromSnapshot(layerSnapshot, activeMarkerLayer.id isnt markerLayerId)
      else
        @markerLayers[markerLayerId]?.restoreFromSnapshot(layerSnapshot)

  emitMarkerChangeEvents: (snapshot) ->
    if @transactCallDepth is 0
