Module Loading Methods
The following methods allow you to register and load your own modules for use by the map control. More information about building your own module is in the Building Your Own Module topic.
Methods
| Name | Definition | Return Value | Description |
|---|---|---|---|
|
loadModule |
|
None |
Loads the specified registered module, making its functionality available. An optional function can be specified that is called when the module is loaded. To register a module, use the registerModule method. The following Bing Maps modules are available: |
|
moduleLoaded |
|
None |
Signals that the specified module has been loaded and if specified, calls the callback function in loadModule. Call this method at the end of your module script. |
|
registerModule |
|
None |
Registers a module with the map control. The name of the module is specified in moduleKey, the module script is defined in scriptUrl, and the options provides the location of a *.css file to load with the module. Tip:
To minimize possible conflicts with other custom modules, choose a unique module name (defined in moduleKey). For example, you can use your company name in the name of the module.
Once you have registered a module, you can make its functionality available by loading it using loadModule. |
Example
The code below shows how to register and load a module, named MyModule1. The code for MyModule1 is found in the next code section.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript">
var map;
function myModuleLoaded()
{
alert("myModule has loaded.");
// Use the function provided by the newly loaded module
var myModule = new MyModule(map);
myModule.HelloWorld();
}
function GetMap()
{
// Initialize the map
var options = {credentials: "Bing Maps Key"};
map = new Microsoft.Maps.Map(document.getElementById('mapDiv'), options);
// Register and load a new module
Microsoft.Maps.registerModule("MyModule1", "http://example.com/mymodule.js");
Microsoft.Maps.loadModule("MyModule1", { callback: myModuleLoaded });
}
</script>
</head>
<body onload="GetMap();">
<div id='mapDiv' style="position:relative; width:400px; height:400px;"></div>
</body>
</html>
// mymodule.js
function MyModule(map)
{
this.HelloWorld = function()
{
alert("Hello World - map Center is " + map.getCenter().toString());
}
}
Microsoft.Maps.moduleLoaded('MyModule1');
Tip: