造型geojson的问题

时间:2014-04-27 08:42:29

标签: javascript leaflet geojson

所以我想从geojson定义样式函数(对于我的观点)使用fillcolor取决于一个属性 - feature.properties.Nab(这意味着宗教)。我只找到了多边形或折线的例子。我不知道问题出在哪里。 TY的帮助和我在这里的第一个问题,所以不要杀了我,如果我问一些微不足道的事情:)。

var geojsonMarkerOptions1 = {
    radius: 7,
    fillColor: getColor(feature.properties.Nab),
    color: "red",
    weight: 1,
    opacity: 1,
    fillOpacity: 0.8
};

function getColor (feature) {
         switch (feature.properties.Nab) {
         case 'Katolici' : return {fillcolor: "blue"};
         case 'Protestanti' : return {fillcolor: "red"};
}
};

L.geoJson(sidla, {
 pointToLayer: function (feature, latlng) {
  return L.circleMarker(latlng, geojsonMarkerOptions1);
 }
}).addTo(map);

//my data:

 var sidla ={
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "properties": { "id": 3, "Nazov": "Lipt_Jan", "Nab": "Katolici", "Nar": "Slovaci" }, "geometry": { "type": "Point", "coordinates": [ 19.677321608721062, 49.049820448418181 ] } },
{ "type": "Feature", "properties": { "id": 2, "Nazov": "Lipt_PETER", "Nab": "Katolici", "Nar": "Nemci" }, "geometry": { "type": "Point", "coordinates": [ 19.733272790197596, 49.053442098858454 ] } },
{ "type": "Feature", "properties": { "id": 1, "Nazov": "Lipt_hradok", "Nab": "Protestanti", "Nar": "Slovaci" }, "geometry": { "type": "Point", "coordinates": [ 19.725950783732767, 49.037323729155595 ] } }
]
}

1 个答案:

答案 0 :(得分:0)

在对象geojsonMarkerOptions1中,您无法访问该功能。它是专门用于为标记指定默认样式的对象。要为每个标记指定不同的样式,您应该为L.geoJSON的样式参数提供一个函数。

这样的事情应该有效:

var geojsonMarkerOptions1 = {
    radius: 7,
    fillColor: "grey",
    color: "red",
    weight: 1,
    opacity: 1,
    fillOpacity: 0.8
};

function getColor (feature) {
         switch (feature.properties.Nab) {
         case 'Katolici' : return {fillColor: "blue"};
         case 'Protestanti' : return {fillColor: "red"};
}
};

L.geoJson(sidla, {
 pointToLayer: function (feature, latlng) {
  return L.circleMarker(latlng, geojsonMarkerOptions1);
 },
 style: getColor
}).addTo(map);