如何在地图框中的层数组中循环以侦听单击事件
How to loop through array of layers in mapbox to listen for a click event?
我是Mapbox API的新手,我正在尝试开发从服务器返回geojson文件并在地图上绘制功能的东西。
我成功地在地图上绘制了特征。我所做的是查看所有记录,看看geojson列是否有值,如果有,我从服务器中获取它,创建一个新层并在该层上绘制它。
这些都是多边形。我需要做的是当我点击其中一个多边形时激发一个事件,这就是我使用单个层的原因。这是代码:
<script type="text/javascript">
L.mapbox.accessToken = 'someKey';
var map = L.mapbox.map('map', 'someMap')
.setView([-39.67, -69.26], 4);
$(document).ready(function(){
$('header h2').text('Equipment Map');
$.getJSON('distributor-companies', function (data) {
var layers = [];
$.each(data, function(i, item) {
if(item.geojson != ''){
var file = item.geojson;
layers[i] = L.mapbox.featureLayer().addTo(map);
$.getJSON('/geojson/' + file, function(data){
console.log('layer' + i);
layers[i].setGeoJSON(data);
});
}
});
$.each(layers, function(i, item){
console.log(item);
// item.on('click', function(e){
// alert('hello');
// });
});
layers[32].on('click', function(e){
alert('hello');
});
layers[31].on('click', function(e){
alert('hello hi');
});
});
});
</script>
现在,我从分销商公司的路线中获取所有数据,每次都制作一个新的图层,并为每个记录绘制数据。所以最后我留下了很多多边形。
最后,我尝试在层上单独注册一个点击事件侦听器,它工作得很好。但在此之前,我试图在层中循环的代码不起作用。console.log函数将所有层显示为对象,但当我试图在项目上注册点击事件时,它不起作用,我的控制台说TypeError项目未定义,但console.log可以很好地使用它。
很明显,我不是JavaScript专家,也没有意识到我做错了什么。我只需要循环浏览所有层,并在它们上放置onclick事件。
您不需要将每个功能添加为一个单独的featureGroup,它会为您做到这一点。只需使用GeoJSON featurecollection对象安装featureGroup:
var featureLayer = L.mapbox.featureLayer(geojson).addTo(map);
然后循环添加的功能,这些功能实际上只是层,并向它们添加onclick事件:
featureLayer.eachLayer(function (layer) {
layer.on('click', function (e) {
// here e.target holds the layer
// and e.target.feature holds the actual feature
alert('Clicked layer ID: ' + e.target.feature.id);
});
});
就这么简单。Plunker的工作示例:http://plnkr.co/edit/31nQWnEnDYzNSmroZB8G?p=preview
如果你想保持原样,你可以在你的代码中这样做:
$(document).ready(function(){
$('header h2').text('Equipment Map');
$.getJSON('distributor-companies', function (data) {
var layers = [];
$.each(data, function (i, item) {
if (item.geojson != '') {
layers[i] = L.mapbox.featureLayer().addTo(map);
$.getJSON('/geojson/' + item.geojson, function (data) {
layers[i].setGeoJSON(data);
// Loop over the added layer
layers[i].eachLayer(function (layer) {
// Add click event
layer.on('click', function (e) {
// Do stuff
alert('Clicked layer ID: ' + e.target._leaflet_id);
});
});
});
}
});
});
});
如果你只想使用一个featureLayer,你可以这样做:
// Add empty featureLayer
var featureLayer = L.mapbox.featureLayer().addTo(map);
// Get URL index
$.getJSON('index.json', function (index) {
// Create empty feature array
var features = [];
// Loop over URLs
$.each(index, function (i, url) {
// Fetch GeoJSON from URL
$.getJSON(url, function (geojson) {
// Push GeoJSON to array
features.push(geojson);
// Check if is last URL
if (i == index.length - 1) {
// Call addFeature with array
addFeatures(features);
}
});
});
});
function addFeatures (features) {
// Set features in featureLayer
featureLayer.setGeoJSON(features);
// Loop over layers in featureLayer
featureLayer.eachLayer(function (layer) {
// Attach click handler
layer.on('click', function (e) {
// Do stuff
alert('Clicked layer ID: ' + e.target.feature.id);
});
});
}
以下是Plunker的作品:http://plnkr.co/edit/dIMn9oiSpfsltq4iiilq?p=preview
- 如何在for循环中添加事件侦听器
- 注册OpenLayers事件时,即使使用匿名函数或绑定,JavaScript关闭也会触发循环内的所有内容
- 在循环中附加事件处理程序时出现浏览器性能问题
- 如何使用for循环添加所有按钮'单击事件
- 事件循环、回调队列和 Javascript 的单线程是如何连接的
- JavaScript运行时事件循环现有技术
- 事件循环的MEAN.JS setInterval进程(从另一个服务器获取数据)
- jQuery触发器事件在一个循环中多次出现
- 单击中每个循环的事件多次激发
- 使用javascript点击事件的循环引用
- jQuery:如何在没有for循环的情况下将事件处理程序应用于$('#text'+'任意整数
- 基于事件在循环中隐藏元素
- 关于动态元素中循环中的事件
- 页面卸载期间的JavaScript事件循环
- 当从jquery的点击事件中的每个循环创建的数组到它之外时,它显示空白数组
- Node.js阻止事件循环
- 循环(事件)中的闭包问题
- 在FullCalendar插件上创建一个循环事件
- Jquery对话框循环事件
- 如何使用angular引导日历为循环事件提供dtStart和until