You should change two lines. The first one is about PyQGIS 3, the other is about Python 3.

1. Change `QgsMapLayerRegistry.instance().mapLayers()` into `QgsProject.instance().mapLayers()`. Please check [this answer](https://gis.stackexchange.com/a/244467/29431) out for more information.

2. Change `layerMap.iteritems()` into `layerMap.items()`. Please check [What is the difference between dict.items() and dict.iteritems()?](https://stackoverflow.com/q/10458437/5569709) and answers for additional information.