How to move a graphic via dragging 4.2

2347
2
01-09-2017 09:28 PM
BernardoArevalo
New Contributor II

I'm making a polyline and placing marker graphics at every path point. However I've been looking around for hours and can't find anything on setting events on drag click on a graphic to redraw the graphic. Does anyone have any insight?

Thanks,

Nardo

Tags (1)
2 Replies
JohnCleveland
New Contributor

Don't know if you're still having the problem, but I am currently having the same issue.  I'm coming from Google Maps API v3 and wow, I'm finding ArcGIS JS 4.3 severely limited in terms of functionality in their API (as of the time of this writing).  Not to mention documentation is just awful compared to Google's API.

If you're still having the problem, this is what I have figured out for my purposes.  It may or may not help you.  Basically you can attach a "drag" event handler to your MapView, then use the hitTest() method to determine whether the event occurred on a graphic that is in your MapView.  Then you can convert to map coordinates, at which point you can remove and then re-add a graphic at the new location.  The inability to just change the geometry of a Symbol is currently the biggest hurdle.  The fact that you actually have to remove and then re-add the graphic is a real pain.

view.on("drag", function(evt) {
    evt.stopPropagation();
    var screenPoint = {
        x: evt.x,
        y: evt.y
    };

    console.log(evt.action);

    view.hitTest(screenPoint).then(function(response) {
        var graphic = response.results[0].graphic;

    if (graphic) {

        console.log(response.results[0].mapPoint);

        var newGraphic = Graphic({
            geometry: response.results[0].mapPoint,
            symbol: new PictureMarkerSymbol({
                url: myUrl,
                width: "48px",
                height: "48px"
            })
        });

        view.graphics.remove(graphic);
        view.graphics.add(newGraphic);
    }});

});

I also got some of this from a Stack Overflow question here.  Basically it confirms that for API 4.0+ you have to remove a Graphic and then re-add it in order to move it.

Let me know if you have figured out anything different or better.  This is not at all an ideal solution, but it's the best I've been able to figure out, and at this point I'm pretty frustrated with the ArcGIS API.

LarsSchmitz
Regular Contributor II

Might be helpful for you, michaellodes

0 Kudos