如何将具有特定类的标记添加到图层或图层组
How to add a marker with a specific class to a layer or layergroup?
>我有几个标记,它们有一个带有css类的名称,例如:markerOne将.markerone作为css类,依此类推。是否可以创建一个函数将这些标记分配给特定的图层或组?像var layerone = $(L.marker).hasClass("markerone"));
并在图层内分配该类的所有标记?我想这样做,以便稍后我可以使用addLayer和removeLayer打开/关闭该层。
我用来显示标记的功能:
function showResourcesByName(name) {
for (var i = 0; i < markers.resources.length; i++) {
var resName = markers.resources[i].name;
if (resName == name) {
var resIcon = icons.resources[i].icon;
var resSize = icons.resources[i].size;
var resPname = icons.resources[i].pname;
var customIcon = L.icon({
iconUrl: resIcon,
iconSize: resSize, // size of the icon
iconAnchor: [resSize[0]/2, resSize[1]/2], // point of the icon which will correspond to marker's location
popupAnchor: [2, -resSize[1]/2] // point from which the popup should open relative to the iconAnchor
});
for (var j = 0; j < markers.resources[i].coords.length; j++) {
var x = markers.resources[i].coords[j].x;
var y = markers.resources[i].coords[j].y;
marker = L.marker([y, x], {icon: customIcon});
marker.addTo(map).bindPopup(resPname);
$(marker._icon).addClass(name)
}
}
}
}
我提到的类是 $(marker._icon(.addClass(name( 中的 (name( 部分,它从标记中获取名称.js:
var markers = {
"resources": [
{
"name": "AITokarServer",
"coords": [
{
"x": -1210.0,
"y": -1770.0
},
{
"x": -1230.0,
"y": -1810.0
},
因此,所有名称为 AITokarServer 的标记都将有一个类。AITokarServer 等。
您可以通过创建自定义标记类来向L.Marker
添加一些功能,以使事情变得更容易。挂钩到 onAdd
函数,以便您可以在标记初始化时自动分配类名。您可以添加一个函数来检查该类名:
L.CustomMarker = L.Marker.extend({
// Overwrite onAdd function
onAdd: function (map) {
// Run original onAdd function
L.Marker.prototype.onAdd.call(this, map);
// Check if there's a class set in options
if (this.options.className) {
// Apply className to icon and shadow
L.DomUtil.addClass(this._icon, this.options.className);
L.DomUtil.addClass(this._shadow, this.options.className);
}
// return instance
return this;
},
// Function for checking class
hasClass: function (name) {
// Check if a class is set in options and compare to given one
return this.options.className && this.options.className === name;
}
});
现在,您可以在初始化标记时轻松应用类名:
var marker = new L.CustomMarker([0, 0], {
'className': 'foobar'
}).addTo(map);
并检查您的标记上是否设置了某个类:
if (marker.hasClass('foobar')) {
// YES! Do stuff
}
也就是说,您实际上不需要向标记中添加类即可将它们拆分为不同的组。数据结构中已有这些组。请考虑以下结构:
var markers = [{
'name': 'Foo',
'coordinates': [{
'lat': -25,
'lng': -25
}, {
'lat': 25,
'lng': -25
}]
}, {
'name': 'Bar',
'coordinates': [{
'lat': -25,
'lng': 25
}, {
'lat': 25,
'lng': 25
}]
}];
要将它们放入不同的组中,请首先创建一个对象来存储这些组,稍后可以将其添加到 layercontrol 中:
var overlays = {};
现在,您可以迭代结构,为每组标记创建图层组并将其添加到其中:
// iterate the structure, handle each group
markers.forEach(function (group) {
// check if there's already a group for this name
if (!overlays.hasOwnProperty(group.name)) {
// new group, create layergroup, store in object and add to map
overlays[group.name] = new L.LayerGroup().addTo(map);
}
// iterate the coordinates for this group
group.coordinates.forEach(function (coordinate) {
// create markers for each coordinate and add to the layergroup
new L.Marker([coordinate.lat, coordinate.lng]).addTo(overlays[group.name]);
})
});
现在,您可以将覆盖对象添加到图层控件,以便可以切换它们:
new L.Control.Layers(null, overlays).addTo(map);
以下是Plunker上的一个工作示例:http://plnkr.co/edit/t0YiJO8RmEdnIKKXugdm?p=preview
如果需要,您仍然可以通过使用上面的自定义标记类并更改坐标迭代器来添加类名,如下所示:
group.coordinates.forEach(function (coordinate) {
new L.CustomMarker([coordinate.lat, coordinate.lng], {
'className': group.name
}).addTo(overlays[group.name]);
})
这是Plunker上的一个工作示例:http://plnkr.co/edit/cHPSLKDbltxr9jFZotOD?p=preview
在传单中,标记没有CSS类。标记具有图标,图标具有className
选项,因此:
var marker1 = L.marker({
icon: L.icon({
className: 'green'
// Other icon options
})
// Other marker options
});
console.log(marker1.options.icon.options.className);
- 将图层添加到另一个图层
- 如何在“控制缩放”下的“打开图层”上添加一些图像
- 添加支持异步重新加载的 Web 图层
- 如何在添加新标记和图层之前清除所有标记和图层的传单地图
- 如何将标记添加到Mapbox图层控件
- 在谷歌地图HTML中简单地添加KML图层
- 添加多个图层
- 在鼠标单击时添加标记并删除打开图层中现有的标记
- 在传单图层组中为空白图块图层添加选项
- 传单 - 根据变量添加和删除列表中的所有图层
- 获取类型将图层添加到地图后出错
- 无法以编程方式向图层添加要素
- 如何将具有特定类的标记添加到图层或图层组
- 尝试将 kml 图层添加到谷歌地图
- 如何在某人的网页上添加自己的图层
- 如何将一个传单标记添加到多个图层组
- 在地图框 API 中向多点标记图层添加标题和描述
- CartoDB:为图层添加infowindow
- 给图层添加矢量蒙版
- 将信息窗口、图例、比例尺和要素图层添加到一起