Adding a HomeButton dijit to a page and manually specifying event handler...

3946
1
Jump to solution
04-16-2016 03:57 PM
JohnBonifas1
New Contributor III

I have tried to add a HomeButton dijit to my page. However, it won't work if I specify any event listeners on the map object. In other words, if I want to add BOTH a HomeButton, AND a task that specifies event listeners on the map object, like this:

  // for home button
        var objHome = new HomeButton({map: divMap}, "HomeButton");
        objHome.startup();
          // this is how you call 'on' style events in the API
        objMap.on("load", function()
        {
          // for coordinates display - event listeners
          objMap.on("mouse-move", showCoordinates);
          objMap.on("mouse-drag", showCoordinates);
          // for home button - when home event is fired, execute home() method - currently doesn't work
          objHome.on("home",objHome.home());
        });
                // for coordinates display
        function showCoordinates(evt)
          {
          //the map is in web mercator but display coordinates in geographic (lat, long)
          var mp = webMercatorUtils.webMercatorToGeographic(evt.mapPoint);
          //display mouse coordinates
          dom.byId("spanCoordinates").innerHTML = mp.x.toFixed(3) + ", " + mp.y.toFixed(3);
        }

...It won't work.

What do I need to modify here, to get the HomeButton to work with the showCoordinates code? Again, before you post the code on the HomeButton class definition page as a solution - that code WILL NOT WORK if you specify an 'on' function on the map object.

Thanx.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JohnBonifas1
New Contributor III

...Never mind, I figured it out.

You can't use the default dijit members of the HomeButton class because of the map object event delegation.

The following code worked:

// define an event handler for the 'home' event on the HomeButton dijit
objHome.on("home", handleHomeButton);
// now implement the handler function.
function handleHomeButton(evt)
{
  // objHome.home(); // you can't call it this way. This method will not work in this context.
  // instead, you call the centerAt method of the map object. strLongitude and strLatitude are floats.
  objMap.centerAt([strLongitude, strLatitude]);
}

View solution in original post

0 Kudos
1 Reply
JohnBonifas1
New Contributor III

...Never mind, I figured it out.

You can't use the default dijit members of the HomeButton class because of the map object event delegation.

The following code worked:

// define an event handler for the 'home' event on the HomeButton dijit
objHome.on("home", handleHomeButton);
// now implement the handler function.
function handleHomeButton(evt)
{
  // objHome.home(); // you can't call it this way. This method will not work in this context.
  // instead, you call the centerAt method of the map object. strLongitude and strLatitude are floats.
  objMap.centerAt([strLongitude, strLatitude]);
}
0 Kudos