簡體   English   中英

僅在調整窗口大小之后才加載bing地圖

[英]bing map loading only after resizing the window

我不太擅長使用JS,並嘗試在我們的網站中使用Bing地圖,但是它幾次顯示地圖,並且大部分時間不顯示地圖。 下面是加載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;
      }

我已經嘗試了從其他stackoverflow查詢中得到的以下幾個步驟,但是它沒有幫助我:-(

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

問題出在您的setTimeout調用中:

您會看到, setTimeout接收2個參數:

  1. 執行功能
  2. 超時(毫秒)

在您的情況下,您使用了setTimeout(this.loadMap(storeData), 2000); 它不會將函數傳遞給setTimeout,而是傳遞給執行結果。 此外,此代碼還將立即執行this.loadMap ,而不是在2秒內執行。

為了解決這個問題,您可以使用:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM