【发布时间】:2016-09-08 11:53:32
【问题描述】:
使用传单有什么方法可以获取已加载瓷砖的边界(东北、西南)?我想请求服务器只为加载的特定图块加载标记,这样当用户平移/拖动地图时,他可以很容易地看到新区域上的新标记。
【问题讨论】:
标签: leaflet
使用传单有什么方法可以获取已加载瓷砖的边界(东北、西南)?我想请求服务器只为加载的特定图块加载标记,这样当用户平移/拖动地图时,他可以很容易地看到新区域上的新标记。
【问题讨论】:
标签: leaflet
您真正想做的是L.GridLayer 的子类。这将允许对加载/卸载的图块进行精细控制,并且是使用私有 L.GridLayer._tileCoordsToBounds() 方法的最佳方式。
通过对已加载标记的一些基本处理,它应该如下所示:
L.GridLayer.MarkerLoader = L.GridLayer.extend({
onAdd: function(map){
// Add a LayerGroup to the map, to hold the markers
this._markerGroup = L.layerGroup().addTo(map);
L.GridLayer.prototype.onAdd.call(this, map);
// Create a tilekey index of markers
this._markerIndex = {};
},
onRemove: function(map){
this._markergroup.removeFrom(map);
L.GridLayer.prototype.onRemove.call(this, map);
};
createTile: function(coords, done) {
var tileBounds = this._tileCoordsToBounds(coords);
var tileKey = this._tileCoordsToKey(coords);
var url = ...; // Compute right url using tileBounds & coords.z
fetch(url).then(function(res){
if (!key in this._markerIndex) { this._markerIndex[key] = []; }
// unpack marker data from result, instantiate markers
// Loop as appropiate
this._markerGroup.addLayer(marker);
this._markerIndex[key] = marker;
done(); // Signal that the tile has been loaded successfully
});
},
_removeTile: function (key) {
for (var i in this._markerIndex[key]) {
this._markerGroup.remove(this._markerIndex[key][i]);
}
L.GridLayer.prototype._removeTile.call(this, key);
}
});
请注意,缩放可能是错误和图形故障的根源(在加载新缩放级别的标记之前,标记被移除,因为磁贴卸载)。小心那个。
【讨论】: