简体   繁体   English

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

[英]bing map loading only after resizing the window

I am not really good at js and trying to use Bing map in our website but it shows the map few times and doesnt show the map most of the time. 我不太擅长使用JS,并尝试在我们的网站中使用Bing地图,但是它几次显示地图,并且大部分时间不显示地图。 Below is the code snippet of loading map function, can someone please let me know whats wrong in this function, I am using this from other application: 下面是加载map函数的代码片段,有人可以让我知道此函数有什么问题,我正在其他应用程序中使用它:

 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;
      }

I have tried below few steps I got from other stackoverflow queries but it didnt help me:-( 我已经尝试了从其他stackoverflow查询中得到的以下几个步骤,但是它没有帮助我:-(

setTimeout(this.loadMap(storeData), 2000);  Microsoft.Maps.Events.addHandler(map,'resize')

The problem is in your setTimeout call: 问题出在您的setTimeout调用中:

You see, setTimeout receives 2 parameters: 您会看到, setTimeout接收2个参数:

  1. A function to execute 执行功能
  2. Timeout in ms 超时(毫秒)

in your case, you used setTimeout(this.loadMap(storeData), 2000); 在您的情况下,您使用了setTimeout(this.loadMap(storeData), 2000); which doesn't pass the function to the setTimeout but the result of the execution. 它不会将函数传递给setTimeout,而是传递给执行结果。 In addition, this code will also execute this.loadMap immediately and not in 2 seconds. 此外,此代码还将立即执行this.loadMap ,而不是在2秒内执行。

To solve this, you can just use: 为了解决这个问题,您可以使用:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM