Select to view content in your preferred language

infoWindow on a GraphicsLayer

3597
4
10-01-2010 06:25 AM
ReneRubalcava
Honored Contributor
I've been spending some time with the JavaScript API, but I'm a little stumped on this one.
Most of the samples I have seen simply use the map.graphics GraphicsLayer to hold graphics. In my case, I have added a new GraphicsLayer so that I can hold persistent graphics on the map. I use map.graphics to hold Geocode Results or temporary graphics.

What I am trying to do is display the infoWindow for the GraphicsLayer I added to the map, but it's not working as I expected.

On page load, after populating my GraphicsLayer, I add it to the map with a defined InfoTemplate.
gLayer = new esri.layers.GraphicsLayer();
gLayer.id = "features";
var graphic = new esri.Graphic(new esri.geometry.Polygon(geometry), symbol, attributes, infoTemplate);
gLayer.add(graphic);
map.addLayer(gLayer);


During it's use, someone can search for an address and I add that result to map.graphics. Then try to display the infoWindow after it has zoomed in.
var pointMeters = esri.geometry.geographicToWebMercator(geocodeResults[0].location);
map.setExtent(esri.geometry.geographicToWebMercator(geocodeResults[0].bestView));
var onMapZoomHandle = dojo.connect(map, "onZoomEnd", function() {
 dojo.disconnect(onMapZoomHandle);
 console.log("add result zoomed");
 var point = esri.geometry.toScreenPoint(map.extent, map.width, map.height, pointMeters);
 map.infoWindow.show(point, map.getInfoWindowAnchor(point)); // throws error
 //map.getLayer("features").infoWindow.show(point, map.getInfoWindowAnchor(point));
 // tried having layer show directly, long shot, but no go
});


This throws an error and a blank infoWindow
exception in animation handler for: onEnd
TypeError: pt is null
[Break on this error] if(!dojo._hasResource["dijit._base.man...h","tr","xx","zh","zh-cn","zh-tw"]);\r\n
arcgis?v=2.1 (line 48


I can click anywhere else on the added GraphicsLayer and get the predefined InfoWindow just fine. Does that InfoWindow display on the map.click() or the graphicsLayer.click()? If so, is there a way I can throw that event after the zoom to mimic the behavior?

Thanks.
0 Kudos
4 Replies
KellyHutchins
Esri Frequent Contributor
Since you are using map.setExtent, try replacing onZoomEnd with onExtentChange to ensure that the event fires when the extent change is complete.

 

var extentHandler = dojo.connect(map, 'onExtentChange', function() {
        dojo.disconnect(extentHandler); ..........
0 Kudos
ReneRubalcava
Honored Contributor
Thanks. That eliminated the error, but the infoWindow is still blank. It's not a deal breaker. I think I can work around it, but was hoping it would be simple.

I'll keep cracking at it.
0 Kudos
KellyHutchins
Esri Frequent Contributor
When I read your initial post I missed the bit about the info template not displaying. In order to display the graphic's attributes in the info window you'll need to populate the maps info window with the graphics content. Here's a link to a sample in the help that shows this:

http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jssamples_start.htm#jssamples/query_showinfo...
0 Kudos
ReneRubalcava
Honored Contributor
Thanks. After some head scratching and looking over API docs, I figured it out.

// will find if the x/y point for an address is inside of a graphic on my GraphicsLayer
var onMapExtentChangeHandle = dojo.connect(map, "onExtentChange", function() {
 dojo.disconnect(onMapExtentChangeHandle);
 console.log("address result zoomed");
 var point = geometry.toScreenGeometry(map.extent, map.width, map.height, pointMeters);
 for (var i in gLayer.graphics) {
  var g = gLayer.graphics;
  if (g.geometry.contains(pointMeters)) {
   var info = map.infoWindow;
   info.setTitle(g.getTitle());
   info.setContent(g.getContent());
   info.show(point, map.getInfoWindowAnchor(point));
  }
 }
});
0 Kudos