2
votes

I'm trying to use the Bing JavaScript Map control in a new (3.0.0-preview6.19307.2) Blazor server-side application. I have had this working on earlier versions of Blazor and in a client-side application.

The current Blazor JavaScript interop guide does not suggest that any new steps are required but when I add the following section in the body of _Hosts.cshtml and call the createMap function I get an error saying

Microsoft.AspNetCore.Components.Browser.Rendering.RemoteRenderer:

Warning: Unhandled exception rendering component: 'Microsoft' is not defined

ReferenceError: 'Microsoft' is not defined at window.createMap (https://localhost:44372/:79:9)

   <script type="text/javascript" src="http://www.bing.com/api/maps/mapcontrol"></script>

   <script>

    var map = null;
    window.createMap = (data) => {
        if (map != null)
            return;
        map = new Microsoft.Maps.Map('#myMap',
            {
                credentials: '... my credentials ...',
                zoom: 1
            }
        );
        Microsoft.Maps.loadModule('Microsoft.Maps.HeatMap');
        return ;
    };
    </script>

This code does work correctly when placed in the index.html of an equivalent client-size Blazor project so I'm guessing some extra step is required to export the map into the Microsoft name-space. Can anyone suggest what I am missing?

1
You could try putting the script tag that references the library in the head of the document - I'm guessing here because you haven't specified where it is currently.Mister Magoo
thanks - it's currently in the body but I've tried it in the head as well and got the same error.NeilMacMullen
So, when the page has loaded, can you check the browser console for errors? Also check in the browser console if you start typing "window.Mic" does Microsoft appear in the autocomplete?Mister Magoo
Actually - thinking about the error - you are calling "createMap" somewhere in your code - where?Mister Magoo
Actually checking the browser console was an excellent suggestion and revealed this warning... "SEC7111: [Mixed-Content] The origin 'localhost:44372' was loaded in a secure context but tried to load an insecure script resource at 'bing.com/api/maps/mapcontrol'." Changing the script source for bing.com to httpS from http solved the problem and the map now renders correctly!NeilMacMullen

1 Answers

1
votes

Answering my own question after help from Mister Magoo

The answer in this particular case appears to be that the Blazore server-side model requires an HTTPS (not HTTP) connection to the bing maps control.