Select to view content in your preferred language

How to move a Graphic with mouse drag?

1008
2
Jump to solution
05-25-2021 12:07 AM
FatmaAkdemir
Frequent Contributor

I have a ruler item with 2 end nodes and I want to enable user to move these nodes and accordingly adjust the ruler item after that move. Is it possible to move a single item and change a line item after it is added to GraphicsOverlay?

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi there Fatma.

I think your best bet for this would to be use one of our builder types, for example, PolylineBuilder

You can initialize one of these builder types with an existing geometry. The general idea being that whenever a user updates the ruler item, you'd take the existing ruler geometry, create a polylineBuilder, move one of the points, and then replace the old ruler geometry with the new one from the builder.

Something like this :

//Get the geometry from the graphic. You may need to cast this for use in the builder, depending on which geometry type you are using for your ruler.
Geometry rulerGeometry = rulerGraphic->geometry();

PolylineBuilder builder(rulerGeometry);
Part* line = builder.parts()->part(0); //Assuming your ruler is a single part.
line->setPoint(pointIndex, newPosition); //pointIndex will likely be zero or one depending on which end of the ruler your user clicks
Polyline newRulerGeom = builder.toPolyline();

//Set the geometry back into the graphic
rulerGraphic->setGeometry(newRulerGeom);


Equally, you could use a builder to construct a new line from scratch every time, which might be simpler, and should be pretty much equally as performant.

PolylineBuilder builder;
builder.addPoint(unchangedNode);
builder.addPoint(editedNode);
Polyline newRulerGeom = builder.toPolyline();

//Then set the geometry on the graphic
rulerGraphic->setGeometry(newRulerGeom);


I hope that helps.

View solution in original post

2 Replies
by Anonymous User
Not applicable

Hi there Fatma.

I think your best bet for this would to be use one of our builder types, for example, PolylineBuilder

You can initialize one of these builder types with an existing geometry. The general idea being that whenever a user updates the ruler item, you'd take the existing ruler geometry, create a polylineBuilder, move one of the points, and then replace the old ruler geometry with the new one from the builder.

Something like this :

//Get the geometry from the graphic. You may need to cast this for use in the builder, depending on which geometry type you are using for your ruler.
Geometry rulerGeometry = rulerGraphic->geometry();

PolylineBuilder builder(rulerGeometry);
Part* line = builder.parts()->part(0); //Assuming your ruler is a single part.
line->setPoint(pointIndex, newPosition); //pointIndex will likely be zero or one depending on which end of the ruler your user clicks
Polyline newRulerGeom = builder.toPolyline();

//Set the geometry back into the graphic
rulerGraphic->setGeometry(newRulerGeom);


Equally, you could use a builder to construct a new line from scratch every time, which might be simpler, and should be pretty much equally as performant.

PolylineBuilder builder;
builder.addPoint(unchangedNode);
builder.addPoint(editedNode);
Polyline newRulerGeom = builder.toPolyline();

//Then set the geometry on the graphic
rulerGraphic->setGeometry(newRulerGeom);


I hope that helps.

FatmaAkdemir
Frequent Contributor

Thank you very much! @Anonymous User  I was already using a similar approach with PolyLineBuilder. I am first resetting the old geometry and create a new one.

if(mpLineGraphic != NULL){
		this->replaceGeometry(Geometry());
	}
	this->addPoint(startPos);
	this->addPoint(endPos);
	if(mpLineGraphic == NULL){
		mpLineGraphic = new Graphic(this->toGeometry(), mpLineSymbol, this);
		mpMapViever->mpRulersOverlay->graphics()->append(mpLineGraphic);
	}
	else{
		mpLineGraphic->setGeometry(this->toGeometry());
	}
0 Kudos