leaflet.js setView() & map.on("zoomend") 循环调用
leaflet.js setView() & map.on("zoomend") cyclic call
我使用 angularjs 和 leaflet.js。
在我的控制器中,我在传单地图缩放上设置了一个监听器:
map.on("zoomend", function () {
refreshData(...); // use the zoom & bound to get data.
});
refreshData()根据多种标准(面)刷新地图中显示的标记,并使用setView()[=30重新调整地图=]. ( refreshData() 也可以被其他函数调用 )
var refreshData = function(){
// lot of thinks...
map.setView(new L.LatLng(lat,lon),zoomlevel);
}
所以当调用 refreshData 时,我想禁用 map.on("zoomend") 监听器,因为它进行循环调用。
怎么做?
我会使用标志变量。
map.on("zoomend", refreshData);
var refreshData = function(){
map.off("zoomend");
// lot of thinks...
map.setView(new L.LatLng(lat,lon),zoomlevel);
map.on("zoomend", refreshData);
}
感谢@pk。的答案。
我必须添加超时才能使其正常工作,因为 map.on("zoomend", refreshData); 是在 调用之后调用的map.setView(new L.LatLng(lat,lon),zoomlevel); 需要一些时间才能完成:
var refreshData = function(){
map.off("zoomend");
// lot of thinks...
map.setView(new L.LatLng(lat,lon),zoomlevel);
$timeout(function () {
map.on("zoomend", refreshData);
},3000);
}
最好的方法是在 setView 上进行回调,但我找不到这样的方法:
var refreshData = function(){
map.off("zoomend");
// lot of thinks...
map.setView(
new L.LatLng(latlon["lat"], latlon["lon"]),
GeographieService.getZoomFromLocalisationtype("continents"),
{'reset' : true }
).then(function (data) {
map.on("zoomend", refreshData);
});
}
有什么想法吗?
我使用 angularjs 和 leaflet.js。
在我的控制器中,我在传单地图缩放上设置了一个监听器:
map.on("zoomend", function () {
refreshData(...); // use the zoom & bound to get data.
});
refreshData()根据多种标准(面)刷新地图中显示的标记,并使用setView()[=30重新调整地图=]. ( refreshData() 也可以被其他函数调用 )
var refreshData = function(){
// lot of thinks...
map.setView(new L.LatLng(lat,lon),zoomlevel);
}
所以当调用 refreshData 时,我想禁用 map.on("zoomend") 监听器,因为它进行循环调用。
怎么做?
我会使用标志变量。
map.on("zoomend", refreshData);
var refreshData = function(){
map.off("zoomend");
// lot of thinks...
map.setView(new L.LatLng(lat,lon),zoomlevel);
map.on("zoomend", refreshData);
}
感谢@pk。的答案。 我必须添加超时才能使其正常工作,因为 map.on("zoomend", refreshData); 是在 调用之后调用的map.setView(new L.LatLng(lat,lon),zoomlevel); 需要一些时间才能完成:
var refreshData = function(){
map.off("zoomend");
// lot of thinks...
map.setView(new L.LatLng(lat,lon),zoomlevel);
$timeout(function () {
map.on("zoomend", refreshData);
},3000);
}
最好的方法是在 setView 上进行回调,但我找不到这样的方法:
var refreshData = function(){
map.off("zoomend");
// lot of thinks...
map.setView(
new L.LatLng(latlon["lat"], latlon["lon"]),
GeographieService.getZoomFromLocalisationtype("continents"),
{'reset' : true }
).then(function (data) {
map.on("zoomend", refreshData);
});
}
有什么想法吗?