In my QGIS3 Plug-in I have this code to draw a line between two points. The code works, but after running it, QGIS zooms somewhere in the middle of Pacific Ocean, when I'd like to zoom on the new layer ("line"). The problem occurs at the last line of this code when I call addMapLayers()
.
def drawLine(self, line_start, line_end):
start_point = QgsPoint(line_start.asPoint())
end_point = QgsPoint(line_end.asPoint())
v_layer = QgsVectorLayer("LineString", "line", "memory")
pr = v_layer.dataProvider()
seg = QgsFeature()
seg.setGeometry(QgsGeometry.fromPolyline([start_point, end_point]))
pr.addFeatures( [ seg ] )
v_layer.updateExtents()
crs = v_layer.crs()
crs.createFromId(4326)
v_layer.setCrs(crs)
QgsProject.instance().addMapLayers([v_layer])
I tried to add iface.zoomToActiveLayer()
but nothing changed. How can I zoom on this new layer avoiding the Pacific Ocean issue?