Calling the Viewer API from the Script Frame

Use this technique when you want the Viewer API calls to be made as a result of an action in the calling page, but you do not want to reload the page. For example, you may have a page that generates a list of locations and you would like the user to be able to jump directly to any location, while leaving the list still available in the task pane.

In this case, your page can load another page in the hidden script frame, using target="scriptFrame" as part of the <a> tag. This requires creating a separate page to load in the script frame and passing the necessary parameters when the page loads.

The Hello Viewer sample application contains a file named gotopoint.php that is designed to run in the script frame. The <body> element is empty, so the page does not produce any output. Instead, it emits a JavaScript function to execute when the page loads. This function calls the ZoomToView() function in the Viewer API. The essential parts of gotopoint.php are:

    <script language="javascript">

    function OnPageLoad()

    {

       parent.ZoomToView(<?= $_GET['X'] ?>,

       <?= $_GET['Y'] ?>,

       <?= $_GET['Scale'] ?>, true);

    }

    </script>

    <body onLoad="OnPageLoad()">

    </body>

To execute gotopoint.php from the task frame, insert code similar to the following:

    $xLocation = -87.7116768; // Or
    calculate values

    $yLocation = 43.7766789973;

    $mapScale = 2000;

    echo "<p><a href=\"gotopoint.php?" .

      "X=$xLocation&Y=$yLocation&Scale=$mapScale\"" .

      "target=\"scriptFrame\">Click to position map</a></p>";

NoteThis technique is also appropriate for calling the Web API without reloading the task pane. See the Modifying Maps and Layers sample for an example.