Basic Viewer - Implement Show x,y coordinates sample

2102
4
03-26-2013 05:29 AM
NewUser
New Contributor
I've been trying to implement some of the javascript api samples in the Basic Viewer Template without much success.  I figured I'd start small and try adding "show x, y coordinates" as it only has a couple functions.  I can not get this wired up properly.  The only thing I can figure is I don't have the event listeners in the correct place.  Does anyone have a working example of this, or could anyone explain wiring this up properly in the basic viewer in a "to dummies" fashion?
0 Kudos
4 Replies
BrettGreenfield__DNR_
Occasional Contributor II
Could you post the code you've tried to implement?  It's going to be difficult to help without seeing what you've got so far.
0 Kudos
NewUser
New Contributor
I've added the showCoordinates function to layout.js, put the <span> tag in the map div of the index.html and put the listeners (onMouseMove & onMouseDrag) in the createMap function in this section of layout.js:

if (map.loaded) {
            initUI(response);
        } else {
            dojo.connect(map, "onLoad", function () {
             //after map loads, connect to listen to mouse move & drag events
            dojo.connect(map, "onMouseMove", showCoordinates);
            dojo.connect(map, "onMouseDrag", showCoordinates);
                initUI(response);
            });
        }
0 Kudos
JohnGravois
Frequent Contributor
i don't understand the exact code logic enough to explain why what you already tried doesn't work, but for code that needs the map to be loaded, I've always had good luck placing it right inside the initUI() function.

function initUI(response) {
    dojo.connect(response.map, "onClick", showCoordinates);
...

function showCoordinates(evt) {
        var mp = esri.geometry.webMercatorToGeographic(evt.mapPoint);
        alert(mp.x.toFixed(3) + ", " + mp.y.toFixed(3));
}
0 Kudos
NewUser
New Contributor
I ended up getting it working by placing the event listening piece of code at the end of the createMap() function.  Seems to work fine.  I could not get it to work in initUI().
0 Kudos