我不是很擅长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(this.loadMap(storeData), 2000); Microsoft.Maps.Events.addHandler(map,'resize')
答案 0 :(得分:0)
问题出在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);