这是我用来在我的网站上绘制传单 map 的函数:
function drawTweetMap(points) {
if (tweetMap != null)
tweetMap.remove();
var london = [51.505, -0.09];
tweetMap = L.map('tweetMap').setView(london, 5);
var cloudmadeUrl = 'http://{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg';
var subDomains = ['otile1','otile2','otile3','otile4'];
var cloudmadeAttrib = 'Data, imagery and map information provided by <a href="http://open.mapquest.co.uk" target="_blank">MapQuest</a>, <a href="http://www.openstreetmap.org/" target="_blank">OpenStreetMap</a> and contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/" target="_blank">CC-BY-SA</a>';
L.tileLayer(cloudmadeUrl,
{attribution: cloudmadeAttrib,
maxZoom: 18,
subdomains: subDomains})
.addTo(tweetMap);
var markers = L.markerClusterGroup();
var points_rand = L.geoJson(points, {onEachFeature: onEachFeature});
markers.addLayer(points_rand);
tweetMap.addLayer(markers);
tweetMap.fitBounds(markers.getBounds());
}
这个函数被周期性调用:
mapWatch = setInterval(function() {
retrieveCurrentTweetPositions()},
numMinutes*60*1000);
在这个函数中:
function retrieveCurrentTweetPositions() {
var points = retrieveCurrentPositions();
var mapInput = translatePointsInMapFormat(points);
drawTweetMap(mapInput);
}
不幸的是,如果这样绘制 map ,结果如下:
在其他问题中我发现可以通过使 map 大小无效来解决这个问题,所以建议在启动时调用这个函数:
function updateTweetMapPosition() {
setTimeout(function(){
tweetMap.invalidateSize(true);}
,500)
}
我这样做了,这解决了第一次绘制 map 时的问题。但是,当我第二次尝试重绘 map 时,结果如下:
有人遇到过这个问题吗?如何重绘 map 以完全加载其内容?
谢谢。
编辑
这是 jsFiddle:http://jsfiddle.net/qzpwvqzk/
最佳答案
我是这样解决问题的
setTimeout(function(){map.invalidateSize(true);},500);
关于javascript - 传单:更新 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29776775/