[GIS] How to zoom to selected feature with QGIS Python Console

pyqgisqgisselect

I want to select a feature by ID and zoom to it. I tried the following but the canvas doesn't change. Any ideas?

canvas = qgis.utils.iface.mapCanvas()
cLayer = canvas.currentLayer()
cLayer.select(51)
canvas.zoomToSelected()

Best Answer

Try this:

canvas.zoomToSelected( cLayer )

The zoomToSelected method accepts a layer as argument, see QgsMapCanvas.

However, I've noticed it should also work without argument. That's strange.

Related Question