Feel like I've been banging my head against the wall here.
I'm writing a plugin. As part of the functionality, I have need to programmatically add a list of annotations to the map.
What I have: list of coordinates (selected using QgsMapToolEmitPoint
), list of corresponding plaintext notes.
Now, I've been trying to use either QgsAnnotationLayer
or QgsAnnotationManager
to create these annotations.
Code for AnnotationLayer
:
<<in constructor>>
self.notelayer = QgsAnnotationLayer(name ="Annotations", options = QgsAnnotationLayer.LayerOptions)
<<inside a method>>
self.notelayer.clear()
for i in range(len(self.xydata)):
annotation = QgsAnnotationPointTextItem.create()
annotation.setText(self.xynotes[i])
annotation.setPoint(QgsPointXY(self.xydata[i][0],self.xydata[i][1]))
QgsProject.instance().annotationManager().addAnnotation(annotation)
self.notelayer.addItem(annotation)
xydata
has the coordinates in float, and xynotes
has the plaintext.
I was getting an insufficient arguments error with this, so I tried using annotation manager instead. That itself had issues.
What I need is a simple example of how to actually use them to make and manage annotations. I couldn't find any examples on the web.