Using Script to Load a Silverlight Map

This documentation is no longer available on MSDN, however it is available as a CHM download.

This topic describes how to load a simple Silverlight map using JavaScript as well as how to use the scriptable map parameters.

Displaying a Map

To begin, load the Bing Maps Silverlight Control by referencing the Bing Maps Silverlight Control XAP file in an object tag on your Web page. Set the “source” param to the Bing Maps Silverlight Control XAP file. The Bing Maps Silverlight Control XAP file reference is https://dev.virtualearth.net/silverlight/mapcontrol/v1/Microsoft.Maps.MapControl.xap. In addition, you need to provide a valid Bing Maps Key. Set your key using another param tag for the object. Information about getting a Bing Maps Key is found in the Accessing the Control Using a Bing Maps Key topic.

Important

The scriptable Bing Maps Silverlight Control must run within a Web site. You can create a Web application project in Visual Studio and run the scriptable map code within a local Web host, or you can use Internet Information Services (IIS) to create a virtual directory and load the scriptable map code within the default Web page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <head>
      <title>Load a Silverlight Map</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   </head>
   <body>
      <div id="silverlightControlHost">
         <object id='slCtl' data="data:application/x-silverlight,"type="application/x-silverlight-2" width="800" height="500">
            <param name="source" value="https://dev.virtualearth.net/silverlight/mapcontrol/v1/Microsoft.Maps.MapControl.xap" />
            <param name="enableHtmlAccess" value="true" />
            <param name="initParams" value="ApplicationId=Your Key"/>
         </object>
      </div>
   </body>
</html>

Using Scriptable Map Parameters

The scriptable Bing Maps Silverlight Control makes some parameters available to allow easy initialization of the map. The table below lists the available initialization parameters. Parameters are set in the values attribute of the param tag and the scriptable initParams are separated by a comma.

initParam Name Description Example

ApplicationId

A valid Bing Maps Key.

ApplicationId=KeyString

Center

Two doubles, which represent the latitude and longitude of a location, separated by an underscore.

Center=47.620574_-122.34942

ClientToken

A valid token retrieved from the Bing Maps Token Service.

ClientToken=TokenSring

ZoomLevel

A double that represents the zoom level of the map view. Valid values range from 1-24.

ZoomLevel=10

The following example sets the initial zoom level and location of the center of the map.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <head>
      <title>Load a Silverlight Map</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   </head>
   <body>
      <div id="silverlightControlHost">
         <object id='slCtl' data="data:application/x-silverlight,"type="application/x-silverlight-2" width="800" height="500">
            <param name="source" value="https://dev.virtualearth.net/silverlight/mapcontrol/v1/Microsoft.Maps.MapControl.xap" />
            <param name="enableHtmlAccess" value="true" />
            <param name="initParams" value="ApplicationId=Your Key, ZoomLevel=10, Center=47.620574_-122.34942"/>
         </object>
      </div>
   </body>
</html>