- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 leaflet market cluster我将标记设置为圆形标记。
Leaflet version: leaflet@1.3.1
Leaflet cluster version: markercluster@1.3.0
注意
$.fn.almDone...
and$.fn.almEmpty...
are just some functions I use for my ajax callbacks
如果我们有结果,我将在 map 上绘制一些标记,如果在第二次回调中我们的结果为零,则清除我们绘制的标记。或者简单地告诉用户我们的结果为零,因此标记为零。
我们有 2 个数组,其中的值是我获取坐标的位置:
var longitude = [];
var latitude = [];
var count = [];
我们在开始时将 var 设置为 stopAjax true
:
var stopAjax = true;
点击我们开始搜索,我们将 stopAjax
设置为 false
$(".alm-filters--button").on("click", function(){
stopAjax = false;
});
这就是基本逻辑,现在我们定义两个函数:
// This is run when we finish the call and we have results
// So on the callback we run the function to draw the markers
$.fn.almDone = function(alm){
drawMarkers();
};
// Let's draw the markers
function drawMarkers() {
// This is the logic to read latitude and longitude arrays
// and push to a new array the two values as pair of coords
// eg. 4.66, 4,5555
var i;
for (i = 0; i < longitude.length; ++i) {
pair=[ parseFloat( latitude[i] ) , parseFloat( longitude[i] ) ]
count.push( pair );
$("#searchNations").removeAttr("disabled");
$(this).attr("disabled", "disabled");
var myYears = $('#years').val();
$("#ajax-load-more ul").attr("data-meta-value", myYears);
};
// We check if we said to run ajax
// and if so draw the markers
// for myself I had also to retrieve those coords
// and place them in individual inputs for the form
if(stopAjax == false) {
L.MarkerCluster.include({
spiderfy: function(e) {
var childMarkers = this.getAllChildMarkers();
this._group._unspiderfy();
this._group._spiderfied = this;
// Fill the markersList.
markersList.innerHTML = childMarkers
.map((marker, index) => `<li>Marker ${index + 1}: ${marker.getLatLng()}</li>`)
.join('');
// If there are any childMarkers
// draw the cluster and run a form
if(childMarkers.length > 0) {
// Match the lat and lng numbers from the string returned by getLatLng()
const [lat, lng] = `${ childMarkers[0].getLatLng() }`.match(/(-?\d+.\d*)/gi);
// Construct the required string value from the extracted numbers
const requiredString = `${ lat }, ${ lng }`;
// Use requiredString to populate the value attribute of the input field in OP
// grab the coords in individual inputs
// that's just for my specific case
$("#longiTude").attr("value",lat);
$("#latiTude").attr("value", lng);
// run a form to see results
submitSearchForm();
}
},
unspiderfy: function() {
this._group._spiderfied = null;
}
});
// this bit is for single marker
// we want to add a click to run the form
// also for the single marker and grab the coords
// and place them in inputs for the form
var mcg = L.markerClusterGroup().addTo(map);
circles = new L.MarkerClusterGroup();
for (var i = 0; i < count.length; i++) {
var a = count[i];
var circle = new L.CircleMarker([a[0], a[1]]);
circles.addLayer(circle);
circle.on('click', function (e) {
var curPos = e.target.getLatLng();
$("#longiTude").val(curPos.lat);
$("#latiTude").val(curPos.lng);
submitSearchForm();
});
}
// we add the markers to the map
map.addLayer(circles);
// we empty the arrays for the future calls
count = [];
longitude = [];
// we set again stopAjax var to true to reset
stopAjax = true;
}
}
然后是零结果函数
//This is run when we have zero results
$.fn.almEmpty = function(alm) {
stopAjax = true;
clearMarkers();
};
// We empty the arrays and we
// clear the previously drawn markers
function clearMarkers(stopAjax) {
if(stopAjax == true) {
count = [];
longitude = [];
map.removeLayer(circles);
}
// if zero results, we launch a modal to advise user
$('#zeroResults').modal('show');
}
如果我们首先有结果,然后我们进行另一次搜索并且结果为零,则上述方法有效,但如果我们首先进行搜索并且我们得到零结果
,那么我们将有一个错误:
Uncaught ReferenceError: circles is not defined
这是正确的,因为我们进入了empty function
,我们试图clear the markers
,我们从来没有定义过,因为我们从来没有进入draw markers function我们定义 circles
的地方。
我对如何绘制标记并使 var circles
在这两种情况下都可用感到非常困惑。
p.s. Happy for anyone to improve the logic regardless of the question issue
最佳答案
我会考虑通过 var circles = undefined;
将 circles
作为两个函数范围之外的变量。请注意,问题不在于 circles 是 undefined
,而是它未定义,即未被识别为变量。
然后像在drawMarkers
中那样设置它。
在调用 removeLayer 之前在 clearMarkers 上检查 if (circles)
以检查它是否已定义。然后在调用 removeLayer 后再次将其设置为 undefined。
关于javascript - 如何在传单 map 上添加和删除标记簇?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53465385/
我有一个带有 LeafletJS 的项目。 例如,我在 map 中有 2 个点 (A, B)。我将其显示为 2 个标记 我必须画一条从 A 到 B 的折线。 我移动了标记 A,我想将标记 A 的折线的
是否可以向自定义图标标记添加文本?我想避免在图像编辑器中编辑图标只是为了添加文本。 我已经像这样创建了我的自定义图标标记: var airfieldIcon = L.icon({ ic
我想要一个 Leaflet 图层控件,选中/取消选中时必须显示/隐藏主图层内的所有子图层。我还想独立检查/取消选中子层。我浏览了 Leaflet 文档和论坛,但找不到任何引用资料。我应该为此编写一个自
如何处理 Leaflet map 库中的重叠线? 我从服务器 sid 下载 geoJSON 并将其绘制到 map 上。如果有两个相同的条目,Leaflet 会绘制它们两次。这可以通过在服务器端查找完全
我在项目中使用 leafletjs。在 map 上我有几个多边形和标记。当其中一个被点击时,所有这些都有一个点击事件来显示一些信息。如果用户单击 map 的“空白”部分(而不是任何多边形或标记),我想
我正在尝试对下一个边界的外观做出预测。这是一个plunkr: https://plnkr.co/edit/sk6NRh51WZA2vpWP 这就是我让它工作的方式,但它不是很有效: const ori
我是 Leaflet 的新手,目前我正在努力学习教程。到目前为止,我设法创建了一个交互式 clorophet map ,就像示例中的 http://leafletjs.com/examples/cho
我很难弄清楚为什么我无法在 map 上绘制正确的国家/地区。我已经完成了所有代码,但我仍然不明白为什么不能正常工作。 如果您看到任何问题,请告诉我。我很欣赏。 这是数据集 Country
是否可以在使用 Stamen Toner-lite tiles 的 Leaflet map 上设置中间(2.5、3.5、4.5 等)缩放级别? ?这是我到目前为止计算缩放级别的代码: leafletm
早上、下午或晚上。 我有以下位置数据(从'Count of sampling points within a grid cell'调整) # Demo data set.seed(123) # lat
大家好,我正在开发一个具有 map 功能的网站。我的目标是,如果您单击标记,则使其拖动。 这是我的代码 else if (select1.value === "Arson"){ var note =
我正在开发一个应用程序,用户可以在其中上传KML文件,并使用Leaflet,toGeoJSON和Angular-Leaflet-Directive在屏幕上呈现路径。 我使用toGeoJSON.kml(
我需要在多边形要素中实现多色填充。填充将根据要素属性进行有条件的格式化。 假设我需要一个具有 3 色图案的多边形,如下所示: let fillPalette = ['orange', 'green',
我从 javascript 开始,并试图解决这个问题。 我希望当用户单击 map 时出现一个表单。当他们填写表单时,我想在该位置创建一个标记,并将该标记的弹出内容设置为表单中的值。添加表单完成后,我还
我已经在地理服务器中发布了一个功能,我可以通过传单及其 basemap 成功访问该功能。现在我需要在弹出窗口中获取feature onclick的属性信息。 我按原样使用了示例( https://gi
我正在使用带有一些标记的传单 map 。我还有一个侧边栏,可以在其中看到标记的名称,如果单击标记的名称, map 将缩放到标记的 lnglat。它已经可以工作,但我的问题是,当我刷新页面并在侧边栏中选
我正在使用 Leaflet 和 Mapbox,我想设置 map 的 View : 所有标记均可见 中心设置为特定点 使用 setView 和 fitbounds 单独完成每个点很容易,但我不知道如何同
是否有所有潜在 map 源的列表?在示例页面上,可以浏览四种类型的 map 。外面还有什么? http://tombatossals.github.io/angular-leaflet-directi
我正在使用 Leaflet 制作交互式 map 。我在 map 上有标记(基于其经度和纬度坐标),并且我希望某些标记具有与其他标记不同的颜色。例如,某些坐标我给它的分数是“10”,我希望它是粉红色的,
我可能忽略了如何在 map 上更改 LeftletJS 的光标。 http://leafletjs.com/reference.htm 我尝试设置map_div.style.cursor = 'cro
我是一名优秀的程序员,十分优秀!