Bing Maps v7: The OnScriptLoad parameter

One positive thing that seems to have come out of the confusion regarding the recent Bing Maps v7 AJAX update has just been mentioned in a forum post by Chris Pendleton, in which he suggests a possible resolution to the lazy-loading errors (“xxx is undefined” / “yyy is not a Constructor function” etc.) is to make use of the OnScriptLoad parameter.

This parameter, which had been present throughout the v6.x versions of Bing Maps, was conspicuous by its absence in v7 and, despite several comments regarding its whereabouts, did not appear anywhere in the documentation for the v7 API. Based on Chris’ comments, it appears that this parameter has in fact always been present in v7, but accidentally omitted from the documentation. So, until that gets corrected, you can refer to the v6.3 documentation, taken from here: http://msdn.microsoft.com/en-us/library/cc980837.aspx

“onScriptLoad is a string parameter that specifies the name of a JavaScript function to call when the map control script is loaded. The name must contain only alphanumeric characters. The CSS and tiles will not be loaded when this function is called. (This parameter is useful for loading the map control script from an UpdatePanel in ASP.NET Ajax. Users can call Sys.Application.notifyScriptLoaded from their callback to tell ASP.NET Ajax the script has loaded.)”

Not only does this parameter provide an apparent workaround for some of the issues currently being experienced with the v7 control, but it also provides a useful method for any other functions in the future that you want to execute only after you are certain that the library has been loaded (note that this is not the same as when the map has been loaded into the page). To take the example from my last post, you can therefore safely create an instance of the Microsoft.Maps.Location class by calling the tryCreateLocation function from onScriptLoad callback, as follows:

[php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Bing Maps v7 Initialisation Bugs</title>
<script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&amp;onscriptload=tryCreateLocation" type="text/javascript"></script>
<script type="text/javascript">
function tryCreateLocation() {
try {
var MyLocation = new Microsoft.Maps.Location(51, -0.15);
alert(MyLocation);
}
catch (e) {
alert(e);
}
}
</script>
</head>
<body></body>
</html>

[/php]

Leave a Reply