Javascript 如何列出用户视图中的Mapbox gridLayer元素?
我想列出Mapbox gridLayer中用户在视口中可见的所有元素 Mapbox站点上有一个名为“”的优秀示例。它显示浏览器视图中featureLayer上的所有标记 我想创建类似的东西,但使用gridLayer代替。(gridLayer是在TileMill中创建的) 下面是一个示例,其中包含一个版本的非工作“界限内”代码: 作为参考,gridLayer中有一个名为“{{{magnize}}}”的变量(从UTFGrid中的TileMill传递),我想列出用户看到的每个地震实例,并将其添加到示例左下角的列表中。我尝试使用的函数是gridLayer.getData(latlng,callback) 以下是不起作用的代码片段:Javascript 如何列出用户视图中的Mapbox gridLayer元素?,javascript,leaflet,mapbox,Javascript,Leaflet,Mapbox,我想列出Mapbox gridLayer中用户在视口中可见的所有元素 Mapbox站点上有一个名为“”的优秀示例。它显示浏览器视图中featureLayer上的所有标记 我想创建类似的东西,但使用gridLayer代替。(gridLayer是在TileMill中创建的) 下面是一个示例,其中包含一个版本的非工作“界限内”代码: 作为参考,gridLayer中有一个名为“{{{magnize}}}”的变量(从UTFGrid中的TileMill传递),我想列出用户看到的每个地震实例,并将其添加到示
map.on('move', function() {
// Construct an empty list to fill with gridLayer elements.
var inBounds = [],
// Get the map bounds - the top-left and bottom-right locations.
bounds = map.getBounds();
// For each part of the grid, consider whether
// it is currently visible by comparing with the current map bounds.
// This is what fails....
myGridLayer.getData(bounds,function(earthquake){
inBounds.push(earthquake.Magnitude);
});
// Display a list of markers.
document.getElementById('coordinates').innerHTML = inBounds.join('\n');
});
如果我犯了一个简单的错误,请道歉…事实上你不能(至少不容易)
1/您误解了gridlayer上的getData函数
- 第一个parm不是一个边界,而是一个位置
- 当您在回调中使用此选项(例如onclick)时,仅当您单击其中一个圆圈时,才会获得数据,如果不单击,则会获得未定义的数据)。数据不是标记列表,而是地震信息(例如{DateTime:“2014-04-26T16:59:15.710+00:00”,震级:3.9})
myGridLayer.getData(event.latlng, function(data) {
console.log(data);
// if clicked on a marker: data is defined
// e.g. Object {DateTime: "2014-04-26T16:59:15.710+00:00", Magnitude: 3.9}
});