Select to view content in your preferred language

why aren't the Graphic object able to add listener on click event

2457
3
07-09-2014 12:34 AM
Aaronsi
Deactivated User

I wonder why the Graphic object aren't able to add listener on click event, then we have to add listener to GraphicsLayer object on click event? Any good reason for that?  The result is if I want to add a graphic on map and add a click event on it, I should add that graphic on a graphicsLayer. Then add click event on graphicsLayer. I think this way is completely inconvenient? Also if there are more then 1000 graphic that I have to add on the map, the map become really slowly! Any help?

0 Kudos
3 Replies
TimWitt2
MVP Alum

Aaron you should be able to have a onclick event for the graphics which you have added to the map, as outlined here.

require(["esri/map", ...], function(Map, ...) {
  var map = new Map("mapDiv"),
  mapOnLoad = map.on("load", function(){
    map.graphics.on("click", myGraphicsClickHandler);
  });
  map.addLayer(...);
  
  function myGraphicsClickHandler(evt) {
    alert("User clicked on " + evt.graphic);
  }
});

I hope this is what you were looking for.

Tim

0 Kudos
Aaronsi
Deactivated User

! !Hi Tim, you've been so helpful! Actually I've already done the same as you said. But I still perfer something like that:

var PictureMarkerInfo = function()

{

    this.jd = 0.0;

    this.wd = 0.0;

    this.imageUrl = "";

    this.imageWidth = 0;

  this.imageHeight = 0;

}

var addPictureMarkerOnMap = function(pictureMarkerInfo)

{

    var graphic;   

  require([

  "esri/geometry/Point",

  "esri/dijit/PopupTemplate",

  "esri/symbols/PictureMarkerSymbol",

  "esri/graphic"

  ], function(Point, PopupTemplate, PictureMarkerSymbol, Graphic){

  var point = new Point(pictureMarkerInfo.jd, pictureMarkerInfo.wd);

  var markerSymbol = new PictureMarkerSymbol(pictureMarkerInfo.imageUrl, pictureMarkerInfo.imageWidth, pictureMarkerInfo.imageHeight);

  var graphic = new Graphic(point, markerSymbol, null, null);

  graphic.on("click", myGraphicsClickHandler); 

  map.graphics.add(graphic);

  function myGraphicsClickHandler(evt)

  { 

  alert("User clicked on " + evt.graphic); 

  }

  });  

    return graphic;

}

Then whether I want to add a listener to a graphic or not,I can do it easily in that way. So I wonder why graphic can't be add a listener. what does the designer want to design for?

0 Kudos
ReneRubalcava
Esri Frequent Contributor

You might be able to do something like this.

var graphicNode = graphic.getNode();

on(graphicNode, 'click', function() {/*stuff*/});

I haven't tested that, but worth a shot.

0 Kudos