Javascript 仅在调整窗口大小后加载bing地图

Javascript 仅在调整窗口大小后加载bing地图,javascript,bing-maps,Javascript,Bing Maps,我不太擅长js,并且尝试在我们的网站上使用Bing地图,但它只显示了几次地图,大部分时间都没有显示地图。下面是加载映射函数的代码片段,有人能告诉我这个函数有什么问题吗,我是从其他应用程序使用这个函数的: function loadMap(storeData) { var coordinates = {}; var map; var stores = storeData.stores; if( (typeof stores !==

我不太擅长js,并且尝试在我们的网站上使用Bing地图,但它只显示了几次地图,大部分时间都没有显示地图。下面是加载映射函数的代码片段,有人能告诉我这个函数有什么问题吗,我是从其他应用程序使用这个函数的:

 function loadMap(storeData) {
        var coordinates = {};
        var map;
        var stores = storeData.stores;
        if( (typeof stores !== 'undefined') && (typeof stores[0].coordinates !== 'undefined') ) {
          coordinates.lat = stores[0].coordinates.lat;
          coordinates.lng = stores[0].coordinates.lng;
        }else {
          coordinates.lat = 33.74831008911133;
          coordinates.lng = -84.39111328125;
        }

        map = new Microsoft.Maps.Map($('#bingMap')[0], {
          credentials: 'mykey',
          liteMode: true,
          enableClickableLogo: false,
          center: new Microsoft.Maps.Location(coordinates.lat, coordinates.lng)
        });

        self.center = new Microsoft.Maps.Location(coordinates.lat, coordinates.lng);        
        map.setView({zoom: 13});

        return map;
      }
我从其他stackoverflow查询中尝试了以下几个步骤,但对我没有帮助:-(


问题出在您的
setTimeout
调用中:

您可以看到,
setTimeout
接收两个参数:

  • 要执行的函数
  • 超时(毫秒)
  • 在本例中,您使用了
    setTimeout(this.loadMap(storeData),2000);
    它不会将函数传递给setTimeout,而是传递执行的结果。此外,此代码还将立即执行
    this.loadMap
    ,而不是在2秒钟内执行

    要解决此问题,您可以使用:

    setTimeout(function(){this.loadMap(storeData)},2000);

    或:(@Sysix的解决方案)
    setTimeout(this.loadMap.bind(this),2000,storeData);

    问题在于您的
    setTimeout
    调用:

    您可以看到,
    setTimeout
    接收两个参数:

  • 要执行的函数
  • 超时(毫秒)
  • 在本例中,您使用了
    setTimeout(this.loadMap(storeData),2000);
    它不会将函数传递给setTimeout,而是传递执行的结果。此外,此代码还将立即执行
    this.loadMap
    ,而不是在2秒钟内执行

    要解决此问题,您可以使用:

    setTimeout(function(){this.loadMap(storeData)},2000);

    或:(@Sysix的解决方案)
    setTimeout(this.loadMap.bind(this),2000,storeData);
    setTimeout(this.loadMap.bind(this),2000,storeData);这起作用了,但是现在PIN消失了,按照这个顺序我调用这些函数,我做错了什么吗this.bingMap=this.loadMap(storeData);this.loadPins(this.bingMap,storeData,true);setTimeout(this.loadMap.bind(this),2000,storeData);
    setTimeout(this.loadMap.bind(this),2000,storeData);
    这个工作了,但是现在pin消失了,按照这个顺序我调用这些函数,我做错了什么吗this.bingMap=this.loadMap(storeData);this.loadPins(this.bingMap,storeData,true);setTimeout(this.loadMap.bind(this),2000,storeData);这起作用了,但现在管脚消失了,按照这个顺序我调用这些函数,我是否做错了什么This.bingMap=This.loadMap(storeData);This.loadPins(This.bingMap,storeData,true);setTimeout(This.loadMap.bind(This),2000,storeData);这起作用了,但现在管脚消失了,在这个序列中,我调用这些函数,我是否做错了什么This.bingMap=This.loadMap(storeData);This.loadPins(This.bingMap,storeData,true);setTimeout(This.loadMap.bind(This),2000,storeData);
    setTimeout(this.loadMap(storeData), 2000);  Microsoft.Maps.Events.addHandler(map,'resize')