变量只包含某些标记 - 如何在传单中进行?

时间:2013-12-25 15:03:47

标签: javascript leaflet

我希望变量hotels仅包含类别为“hotel”的标记。 我有这个代码,但它不起作用(它返回一些传单错误)。

var hotels = L.geoJson(places, {
    pointToLayer: function (feature, latlng) {
      if (feature.properties.category == 'hotel'){
        return L.marker(latlng, {icon:hotelIcon});
      }
      else {
        return false;
      }
    }
}).addTo(map);

或者还有其他更好的方法吗?

我只是初学者,所以感谢您的耐心等待。

1 个答案:

答案 0 :(得分:0)

您将返回两种不同的类型:标记或布尔值(false),而Leaflet不知道如何处理布尔值。

您应首先过滤您的地点,仅包含酒店。以下是使用underscore的过滤函数的示例:

var hotelPlaces = {
    "type" : "FeatureCollection",
    "features" : _.filter(places.features, function(p) { 
        return p.properties.category == 'hotel';
    })
};

var hotels = L.geoJson(hotelPlaces, {
    pointToLayer: function (feature, latlng) {
        return L.marker(latlng, {icon:hotelIcon});
    }
}).addTo(map);