[英]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个参数:
在您的情况下,您使用了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.