pmiranda
pmiranda

Reputation: 8430

ArcGIS JS-API, add InfoTemplate to an existing Graphic

How can I add an infoTemplate to an existing Graphic instance?

I'm doing this graphic instances without infoTemplate for every point that I have:

const layerMarkers = new GraphicsLayer({id: layerId});
this.map.addLayer(layerMarkers);
// Some code
const graphic = new Graphic(point, imageSymbol, null, null );
layerMarkers.add(graphic);

Then I want to call some API when I click the graphic:

layerMarkers.on('click', function(e) {
    console.log(e);
    console.log(this);
    //Here I can see the object, I need to do something here with it
});

What I need is to set:

infoTemplate.setTitle(result.poi.nombre);
infoTemplate.setContent(this.getTooltip(result.poi));

And then I need to update my graph object with the infoTemplate

I can't do:

const graphic = new Graphic(point, imageSymbol, null, infoTemplate);

Or I will overwrite my object.

Any tips?

Upvotes: 1

Views: 111

Answers (1)

Ryan
Ryan

Reputation: 827

Just use the setInfoTemplate method on the graphic layer to set the template after the graphics layer is already instantiated.

If you just want to show the info window when a user clicks on the graphic layer then just use the show method on the maps InfoWindow instance.

this.map.infoWindow.show(e.screenPoint,e.getInfoWindowAnchor(e.screenPoint));

after you set the content.

You can also use setFeatures which will take a deferred (that eventually returns an array of graphics)

There's a bunch of ways to do what you want depending on the order of the user interactions and whether you are getting data for the popup over the network or not.

Upvotes: 1

Related Questions