javascript - 为什么 Leaflet.LayerGroup.Collision 不适用于我的 L.GeoJSON?
问题描述
尝试使用插件"Leaflet.LayerGroup.Collision.js"
。我看不到错误是什么,因为它应该在发生碰撞时隐藏文本。所有文本都显示出来,但仍然相互碰撞,在地图上看起来很乱。
下面的示例中可能有什么问题?我试图尽可能地遵循说明,但似乎缺少一些东西!
var point_txt = new L.layerGroup();
function filt_point(feature) {
if (feature.properties.size === "villages") return true;
}
var collisionLayer = L.LayerGroup.collision({ margin: 8 });
$.getJSON("/data/city_villages.geojson", function(json) {
var pointLayer = L.geoJSON.collision(null, {
filter: filt_point,
pointToLayer: function(feature, latlng) {
label = String(('<span class="textLabelclassmall">' + feature.properties.Namn + '</span>');
return new L.marker(latlng, {
icon: createLabelIcon("textLabelclasssmall", label)
});
}
});
var createLabelIcon = function(labelClass, labelText) {
return L.divIcon({
className: labelClass,
html: labelText
});
};
pointLayer.addData(json);
collisionLayer.addLayer(pointLayer);
collisionLayer.addTo(point_txt);
});
样式.css:
.textLabelclassmall{
left: 1px;
top: -10px;
background-color: transparent;
display: inline-block;
text-align: left;
white-space:nowrap;
letter-spacing: 0.1em;
font-weight: 500;
font-size: 0.5vw;
}
解决方案
的实例Leaflet.LayerGroup.Collision
期望添加到自身的层是L.Markers
,而不是L.LayerGroup
(或派生类的实例,例如L.GeoJSON
)的实例 - 它根本没有为该用例做好准备。
在创建单个标记时添加它们,或考虑L.GeoJSON.Collision
改用它们。
推荐阅读
- python - 将 Raspberrypi 连接到另一个 raspberrypi,然后使用 Xbees 将第二个连接到笔记本电脑
- r - 如何在给定 y 值的情况下预测 x 的值(包括先前的 x 和 y 值)
- javascript - 使用 JSON 数组的 6 级级联下拉列表
- java - 必须为此操作提供 PartitionKey 值
- r - R中预测学生成绩的回归模型
- bash - 从预接收钩子提交 - 错误:未能推送一些参考
- python - 在列表中链接自定义函数的 Pythonic 方法
- gitlab - Omnibus Gitlab SSH 连接问题
- sql - 在 SSRS 中使用我希望成为日期/时间变量的参数设置数据驱动订阅
- node.js - Socket.io 和 Express 与 nginx