2016-03-13 28 views
0

Hiya我试图过滤来自CSV的omnivore.js提供的一些标记。我想按照这个啧啧https://www.mapbox.com/mapbox.js/example/v1.0.0/markers-with-multiple-filters/如何过滤来自Omnivore(csv)提供的Mapbox.js标记

但我不断收到一个错误:

markers.setFilter is not a function

<script> 
    L.mapbox.accessToken = '*******'; 
    var map = L.mapbox.map('map', 'mapbox.streets').setView([38, -95], 4); 

    var markers = omnivore.csv('/test.csv', L.mapbox.featureLayer()) 
     .on('ready', function(layer) { 

      this.eachLayer(function(marker) { 
       if (marker.toGeoJSON().properties.AGE > '70') { 
        marker.setIcon(L.mapbox.marker.icon({ 
         'marker-color': '#258369', 
         'marker-size': 'large' 
        })); 
       } else { 
        marker.setIcon(L.mapbox.marker.icon({})); 
       } 
       marker.bindPopup(marker.toGeoJSON().properties.FN + ', ' + 
        marker.toGeoJSON().properties.LN + ', ' + marker.toGeoJSON().properties.GENDER); 
      }); 
     }).addTo(map); 
    console.log(markers); 

    $('.menu-ui a').on('click', function() { 
     // For each filter link, get the 'data-filter' attribute value. 
     var filter = $(this).data('filter'); 
     $(this).addClass('active').siblings().removeClass('active'); 
     markers.setFilter(function(f) { 
      // If the data-filter attribute is set to "all", return 
      // all (true). Otherwise, filter on markers that have 
      // a value set to true based on the filter name. 
      return (filter === 'all') ? true : f.properties[filter] === true; 
     }); 
     return false; 
    }); 

</script> 

我的CSV包含一些特性,这是想什么我用来筛选这个结果。当我看到它们的标记在的层数 - >要素 - >属性中传递给数组时。

现在教程和我的实现之间的区别是我使用Omnivore而不是setGeoJson。我不知道它是否重要,但这包含在从laravel 5.2 的blade.php模板中。在此先感谢您的帮助。

回答

0

我怀疑是因为你的markers变量分配的omnivore.csv()的结果,这是正常L.GeoJSON对象,而不是一个mapbox特征层。

leaflet-omnivore API说:

By default, the library will construct a L.geoJson() layer internally

这就是为什么你不能使用markers.setFilter()

你可以简单地分配你的markers变量,新的L.mapbox.featureLayer()事前,并把它传递为omnivore.csv第三个参数(customLayer)。

请注意,您还遗漏了第二个参数parser_options(请参阅传单-omnivore API)。

var markers = L.mapbox.featureLayer(); 

omnivore.csv('/test.csv', {}, markers).on(/* etc. */); 

如果这不能解决您的问题,请尽量提供的jsfiddle或Plunker再现您的问题和错误消息。

+0

谢谢你的回复,在这一行Omnivore应该是通过他们的精选层。 var markers = omnivore.csv('/ test.csv',L.mapbox.featureLayer()) 除非我误解了,有没有办法来证明标记在哪里呈现? – jsrosas

+0

看起来像你使用的风格应该工作:https://www.mapbox.com/mapbox.js/example/v1.0.0/filtering/和http://plnkr.co/edit/qws829IXjTkysnWvqJlR?p=preview。我上面描述的风格当然是:http://plnkr.co/edit/dPqlnqXVdAJJDfSPvZss?p=preview。所以你的主要错误很可能是缺少第二个参数。 – ghybs