Javascript 动态小叶层类

Javascript 动态小叶层类,javascript,svg,leaflet,geojson,Javascript,Svg,Leaflet,Geojson,这是我的场景:我有一个带有点特性的geojson,一些带有属性“救护车”,另一些是“干预”。我将使用pointToLayer将它们添加到地图上 var geojsonLayer = L.geoJson(cars, { pointToLayer: function(feature, latlng) { return new L.Marker(latlng, {icon: cssIcon}); }, onEachFeature: onEachFeature }); cssIcon变

这是我的场景:我有一个带有点特性的geojson,一些带有属性“救护车”,另一些是“干预”。我将使用pointToLayer将它们添加到地图上

var geojsonLayer = L.geoJson(cars, {
pointToLayer: function(feature, latlng) {
    return new L.Marker(latlng, {icon: cssIcon});
    }, onEachFeature: onEachFeature });
cssIcon变量使我能够将SVG用于我的点

var cssIcon = L.divIcon({
      className: "css-icon",
      html: "<svg> my svg code here </svg>"
      ,iconSize: [20,20]
      ,iconAnchor: [20,20]});
。。。如果addClass函数应查询功能,请检查功能的属性是否为“救护车”或“干预”,并相应地添加一个类:

function addClass(e){
    var layer = e.target;
    if(layer.feature.properties.car_type === "ambulance"){
    L.DomUtil.addClass(layer.defaultOptions.icon.options, "ambulance-class");

}else(layer.feature.properties.car_type === "intervention") {
    L.DomUtil.addClass(layer.defaultOptions.icon.options, "intervention-class");
}};
我得到的是:

  • 具有“救护车”属性的层将获得“救护车类”类,但是
  • 具有“干预”属性的层将获得“干预类”,也将获得“救护车类”
我还尝试:

 geojson_layer.eachLayer(function (layer) {  
  if(layer.feature.properties.car_type === "ambulance") {    
    L.DomUtil.addClass(layer.defaultOptions.icon.options, "ambulance-class"); 
  }});
…但这根本不会添加类。我使用
layer.defaultOptions.icon.options
添加类可能是错误的,但是使用它我可以通过
document.getElementsByClassName(“救护车类”)获取对象。

有什么想法吗

如果调用单独的函数在
pointToLayer
中创建图标,则可以检查功能属性并将适当的类附加到
className
中:

function getCssIcon(feature) {
  if (feature.properties.car_type === "ambulance") {
    classTxt = " ambulance-class";
  } else if (feature.properties.car_type === "intervention") {
    classTxt = " intervention-class";
  }
  return L.divIcon({
    className: "css-icon" + classTxt,
    html: "<svg> my svg code here </svg>",
    iconSize: [20, 20],
    iconAnchor: [20, 20]
  });
}

var geojsonLayer = L.geoJson(cars, {
  pointToLayer: function(feature, latlng) {
    return new L.Marker(latlng, {
      icon: getCssIcon(feature)
    });
  },
  onEachFeature: onEachFeature
}).addTo(map);
函数getCssIcon(功能){
如果(feature.properties.car_type==“救护车”){
classTxt=“救护车类”;
}else if(feature.properties.car_type==“干预”){
classTxt=“干预类”;
}
返回L.divIcon({
className:“css图标”+classTxt,
html:“我的svg代码在这里”,
iconSize:[20,20],
iconAnchor:[20,20]
});
}
var geojsonLayer=L.geoJson(汽车{
pointToLayer:功能(特性、latlng){
返回新的L.标记(板条{
图标:getCssIcon(功能)
});
},
onEachFeature:onEachFeature
}).addTo(地图);

这就是我最后要做的。
function getCssIcon(feature) {
  if (feature.properties.car_type === "ambulance") {
    classTxt = " ambulance-class";
  } else if (feature.properties.car_type === "intervention") {
    classTxt = " intervention-class";
  }
  return L.divIcon({
    className: "css-icon" + classTxt,
    html: "<svg> my svg code here </svg>",
    iconSize: [20, 20],
    iconAnchor: [20, 20]
  });
}

var geojsonLayer = L.geoJson(cars, {
  pointToLayer: function(feature, latlng) {
    return new L.Marker(latlng, {
      icon: getCssIcon(feature)
    });
  },
  onEachFeature: onEachFeature
}).addTo(map);