"Migration Troubles: Migrating from Bing Map V7 to V8 and Facing Script

After searching for a solution to this issue, I still can't figure out why it's not working even though I've seen similar topics. The code I'm using is quite basic and here's an example of what's causing the problem:

main.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="main.js"></script>
</head>

<body>
    <div id="myMap" style="position:relative;width:600px;height:400px;"></div>
</body>
<script>
    var url = 
    //'http://www.bing.com/api/maps/mapcontrol?branch=experimental&callback=GetMap';
    'https://www.bing.com/mapspreview/sdk/mapcontrol';
    loadScript(url, test_function);
    function test_function() {
        console.log('inside test_function before map');
        map = new Microsoft.Maps.Map('#myMap', {
            credentials: 'My Bing Maps Key'});
        console.log('inside test_function after map');
    };
</script>

</html>  

main.js

function loadScript(url, callback){

    var script = document.createElement("script")
    script.type = "text/javascript";
    script.async = true;
    script.defer = true;

    if (script.readyState){  //IE
        console.log('inside IE block');
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        console.log('inside Other block');
        script.onload = function(){
            callback();
        };
    }

    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

Console output

main.js:18 inside Other block
main.html:17 inside test_function before map
mapcontrol:11 Uncaught TypeError: Cannot read property 'prototype' of null
    at k (mapcontrol:11)
    at n.h [as create] (mapcontrol:11)
    at e (mapcontrol:11)
    at t.l [as instance] (mapcontrol:11)
    at n.h [as create] (mapcontrol:11)
    at e (mapcontrol:11)
    at t.l [as instance] (mapcontrol:11)
    at new Microsoft.Maps.Map (mapcontrol:13)
    at test_function (main.html:18)
    at HTMLScriptElement.script.onload (main.js:20)

I've tried various methods like jQuery getScript but haven't had any luck. Your feedback would be greatly appreciated!

Answer №1

Let me start by suggesting that it might be a good idea to remove your credentials from your question when posting it publicly.

The error seems to be caused by trying to interact with the map before it has finished loading.

I haven't come across a script being loaded in this way before, but I suppose it can work in some cases, although probably not in this one. There must be a specific reason for using this approach.

From what I can gather with my limited knowledge, it seems like the script is loaded and your callback function (test_function) is being executed as expected based on the code.

However, loading the script and loading Bing Maps appear to be two separate processes in this case.

The commented out URL initially had &callback=GetMap, which is the standard way of asynchronously loading Bing Maps. Your GetMap function would be triggered once Bing Maps finishes loading. In this scenario, callback=test_function is what you should use.

Therefore, test_function should be called by Bing Maps itself, rather than passing it through your script loading function (assuming how it works). The purpose of the loading function seems to be solely placing the script without requiring a callback method; Bing Maps will take care of that part.

Here's a working example on Plunker for reference.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method for handling an array in Angular when all of them are successfully passed?

tasks: [ {failed: true, remarks: "",task: {'name': 'task1'}}, {failed: true, remarks: "",task: {'name': 'task2'}}, ] Is there a way to determine the overall status of all tasks based on their suc ...

Troubles encountered when populating the array within a Vue component

I am experiencing an issue with my ProductCard component where the addItem method is not successfully adding a new item to the array. <template> <div class="card"> <div v-for="item in TodoItems" :key="item.id ...

Data-binding in Angular ceases to function properly in the presence of duplicate values within an

My HTML code is set up to bind the $scope.comments array to an unordered list; <div ng-app="" ng-controller="commentController"> <ul> <li ng-repeat="c in comments"> {{ c }} </li> </ul> < ...

Unable to modify the value of an HTML dropdown list

My latest project is a website located at pg.wcyat.me (Source code), where I utilized github.com/thdoan/pretty-dropdowns for dropdown menus. Using JavaScript, I am able to dynamically change the values of select lists. For example: document.getElementById( ...

Is there a way to determine if the items in an array are duplicated?

Hello all, as a beginner in coding, I'm looking for guidance on determining whether an array contains repeated objects regardless of their order, without knowing the specific objects in advance. Let's say: I initialize an empty array: var rando ...

What is the best way to focus on an object using a particular variable in javascript?

I am currently developing an online game where each user is assigned a unique ID. Below is the client code used to create a new player: const createNewPlayer = (id) => { return player[player.length] = { x:0, y:0, id:id } } The ...

Unleashing the potential of extracting the value of a subsequent iteration while within the

Currently, I am facing a dilemma as I am unable to comprehend the logic required to design this algorithm. The problem at hand involves a sequence of images with arrows placed alternatively between each image. The structure appears as follows: Image -> ...

Sliding out list elements with jQuery ladder effect

I'm stuck on a seemingly simple task and need some guidance. facepalm. Currently, my site at this link has a menu list item issue where they all come out from the left side after the picture loads. I want them to slide out one by one, starting from t ...

Generating an assortment of specific rows by utilizing checkboxes in React JS

I'm currently dealing with a table of data where the first column consists of checkboxes. My goal is to generate an array containing the ID of each row that is selected. const [selectedRows, setSelectedRows] = useState([]); const handleCheckbox ...

Incorporating a new Autodesk Forge extension

Hey there! I'm currently working on integrating a forge extension into my viewer, but I seem to have missed something along the way. I've been following this helpful guide: Here's the code snippet that I'm using: <body> < ...

Is it possible to update the page URL and content seamlessly without having to reload the entire page?

I am looking for a solution where I can click on a link (like test.com/page?=test) and have the tab in my browser open the link without reloading or refreshing the page, while also updating the content to match the linked page. How can I achieve this? I ...

Unable to locate the JavaScript files within the NextJs and ReactJs project

I've encountered an issue when trying to import js files (which are libraries) in my project. I am currently using NextJS version 14.1.3 and ReactJS version 18.2.0. You can find the path to these files here Here is a glimpse of the project structure ...

Updating the div#content dynamically with Jquery without the need to refresh the page

After spending countless hours on this forum, I have yet to find a solution that perfectly fits my needs, so I will pose my question. Here is the gist of what I am attempting to accomplish: When the page loads, the default page fades in and displays. Wh ...

What is the most efficient way to dynamically alter the position of a div element within an HTML document using

In a scenario where a parent div contains multiple child divs, it is required that one particular child div named .div_object_last always stays at the end. Below is an example setup : HTML <div class="div_parent"> <div class="div_object"> ...

Generate HTML elements within an AngularJS directive and establish a click event

Can you show me how to create some DOM elements and add a click event to them using an AngularJS directive? My current approach is as follows: var list = document.createElement("div"); list.classList.add('myList'); for(var i = 0; i < n; i++) ...

Utilizing a variable in one function within a separate function (Guide to Calculating Combinations)

As a beginner in JavaScript, I'm struggling with using a variable from one function in another function. My current project involves creating a combination calculator, but I'm facing issues with getting the output when using this script code. ...

Adjust the top margin of a div to match the height of the screen within an iframe, ensuring cross-browser

Trying to adjust the margin-top of a div to 100% of screen height within an iframe seems to be causing issues with jQuery, as it either returns 0 or inaccurate values. While CSS3's 100vh can work as an alternative, it may not be supported in older an ...

To link the information within the angularJS controller

I've recently generated a div element dynamically within the controller function of my AngularJS application. However, I'm facing an issue where the data is not binding as expected inside this div element. Here is a snippet of my code: function ...

The reason behind my struggle with mapping API responses to an Interface in Angular

Hello everyone, I am currently working on mapping some API responses from The Muse API to an APIResponseInterface that I have created. Here's a snippet of my code: Service that sends the request: getJobs(page: number): Observable<APIResponseInterf ...

Has Angular been assimilated into the window object by webpack?

I'm encountering a puzzling issue with webpack that I can't seem to resolve. Here is the link to my webpack.config file: https://github.com/saike/maluvich-browser/blob/master/webpack.config.babel.js In my project, I import angular using ES6 mo ...