Cannot read property 'toScreen' of undefined

5079
7
10-06-2015 04:20 PM
AndrewTerwiel
Occasional Contributor II

I have this simple chunk of code that fails with the error:

"TypeError: Cannot read property 'toScreen' of undefined at p.show (http://js.arcgis.com/3.14/init.js:199"

It fails at this.map.infoWindow.show(selectedAddress[0].geometry, selectedAddress[0].geometry);

              

var infoTemplate = new InfoTemplate();
infoTemplate.title = "Address";
infoTemplate.content = "${ADDRESS}</br>${LOCALITY}";
selectedAddress[0].setInfoTemplate(infoTemplate);
this.map.infoWindow.setFeatures(selectedAddress);
this.map.infoWindow.show(selectedAddress[0].geometry, selectedAddress[0].geometry);

Can anyone see what I'm doing wrong?

Message was edited by: Andrew Terwiel

I have it working now, but this looks like a hack to me.           

var that = this.map._mapParams.infoWindow;
var infoTemplate = new InfoTemplate();
infoTemplate.title = "Address";
infoTemplate.content = "${ADDRESS}</br>${LOCALITY}";
selected[0].setInfoTemplate(infoTemplate);
that.setFeatures(selected);
that.show(selected[0].geometry);
0 Kudos
7 Replies
OwenEarley
Occasional Contributor III

The documentation for InfoWindow states that the location:

is an instance of esri.geometry.Point. If the location has a spatial reference, it is assumed to be in map coordinates otherwise screen coordinates are used.

I would suggest using map.toScreen() to convert your geometry to a screen coordinate. Your last line of code would change to something like:

var screenPt = this.map.toScreen(selectedAddress[0].geometry);
this.map.infoWindow.show(screenPt, this.map.getInfoWindowAnchor(screenPt));

See this example - https://developers.arcgis.com/javascript/jssamples/query_charts.html

0 Kudos
AndrewTerwiel
Occasional Contributor II

Thanks, Owen. See my edit for the fix.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Andrew,

  Is there a reason you are using InfoTemplate instead of PopupTemplate? The maps default infoWindow is a Poppp dijit and would normally be expecting a PopupTemplate.

this.map.infoWindow.show(point); The show method can take a standard esri point geometry and does not require it to be in screen coordinates.

var popupTemplate = new PopupTemplate();
popupTemplate.title = "Address";
popupTemplate.content = "${ADDRESS}</br>${LOCALITY}";
selectedAddress[0].setInfoTemplate(popupTemplate);
this.map.infoWindow.setFeatures(selectedAddress);
this.map.infoWindow.show(selectedAddress[0].geometry);
0 Kudos
AndrewTerwiel
Occasional Contributor II

Hi Robert, I was using InfoTemplate out of ignorance. However, now that I'm using PopupTemplate the title is not displaying. When I use the developer tools, I see that the title has been applied to the template, but I can't see why it doesn't show in the popup. By the way, how do I get my code snippets to look nice like I see in other posts?

var that = this.map._mapParams.infoWindow;
var popupTemplate = new PopupTemplate();
popupTemplate.title = "Address";
popupTemplate.content = "${ADDRESS}</br>${LOCALITY}";
selected[0].setInfoTemplate(popupTemplate);
that.setFeatures(selected);
that.show(selected[0].geometry);
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Andrew,

  Not sure why you are using:

var that = this.map._mapParams.infoWindow;

you should always avoid using underscore functions or properties.

Something like this should work fine:

var popupTemplate = new PopupTemplate();
popupTemplate.setTitle = "Address";
popupTemplate.setContent = "${ADDRESS}</br>${LOCALITY}";
selected[0].setInfoTemplate(popupTemplate);
this.map.infoWindow.setFeatures(selected);
this.map.infoWindow.show(selected[0].geometry);

For code formatting see this link:

Re: How to format a code sample on GeoNet?

0 Kudos
AndrewTerwiel
Occasional Contributor II

When I used this.map.infowindow.show() I get this error:

"TypeError: Cannot read property 'toScreen' of undefined at p.show (http://js.arcgis.com/3.14/init.js:1639:501)..."

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Andrew,

   Strange. I use this.map.infoWindow all the time in my widgets.

0 Kudos