5

I am working on Bing Maps Version 8.0 upgrade from Version 6.3. Here is my upgraded code below which is throwing an error:

var Points = [new Microsoft.Maps.Location(0, 0), new Microsoft.Maps.Location(0, 0)]

The above lines after it executes gives me an error saying

"TypeError: Microsoft.Maps.Location is not a constructor"

The above problem happens in Google Chrome browser. But the same works fine in Internet Explorer.

Any suggestion is helpful.

Thanks in advance.

Regards, Rahul

Shyam
  • 53
  • 1
  • 5
  • Getting same error in chrome.. has anyone experienced the same issue and have solution to it? – meen Jul 10 '17 at 11:41

1 Answers1

4

The issue is likely that you are trying to use the Microsoft.Maps namespace before the map control script is loaded. The V8 map script loads asynchronously for performance, however this means that if you try to access namespace in code right after the script tag to load the map control, the namespace won't be there. You need your code to wait until the map script is loaded. You can handle this by adding a callback function name to the map script tag URL. For example:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type='text/javascript'>
    var map;

    function GetMap()
    {
        map = new Microsoft.Maps.Map('#myMap', {
            credentials: 'YourBingMapsKey'
        });

        //Add your post map load code here.
    }
    </script>

    <script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>
</head>
<body>
    <div id="myMap" style=";width:800px;height:600px;"></div>
</body>
</html>

We also recommend adding the map script tag after your map load code. The reason for this is that if the page is refreshed, the map script will be cached and will call the callback function right away, so the callback function needs to already be loaded.

rbrundritt
  • 16,570
  • 2
  • 21
  • 46
  • Thanks for your suggestion! but it works/loads fine in IE. Any reason why it is failing in Chrome ? – Shyam May 31 '17 at 04:59
  • Browsers load resources slightly differently, this is likely the reason. You may see the issue in IE from time to time as well, perhaps when you press the refresh button. – rbrundritt May 31 '17 at 16:51